I'm in a beginner's c++ course. Currently we are working on a project that will count the number a times each character of the alphabet is in a phrase that the user inputs. The program will output everything onto a table (left to right: Letter | # of occurrences | percentage that the specific character is of the entire phrase | histogram representing the percentage to the nearest percent )
This is THE most pernicious,
puzzling programming project yet,
but at least we can work in pairs.
^D
A 5 6.4% ******
B 1 1.3% *
C 3 3.8% ****
D 0 0.0%
And so on and so forth.
I'm running into the error with invalid operands of types 'double' and 'double' to binary operator%
I understand that it has to do with the % operator in my code, but I'm not entirely sure that I understand how I am using it incorrectly and if I could get some kind of explanation.
The following is the function it is regarding to:
void printchar ( const char s[], int length, double freq[], const char letters[ )
int j = 0;
int i = 0;
double percent = freq[i]/length;
for ( i = 0; i < 26; i++ )
{
cout << letters[i] << setw(5) << freq[i] << setw(5)
<< setprecision(1) << fixed << percent;
if(percent % 1.0 < .5)
cout << "*";
for( j = 0; j <= percent; j++)
cout << "*";
}
I know there's a lot of cleaning up to do, but I'm trying to figure out how I'm using the % operand incorrectly. Any informative responses will be much appreciated.
Thanks
You are trying to get the modulo (remainder) with
percent
which is declared/calculated asdouble
. The modulo '%' symbol requires the operands to be integers i.e.op1 % op2
- both op1 and op2 need to be integers. AFAIK it doesn't cleverly round it to double (as you probably expected it to do). Try and use integer type/alternative method instead.