-1

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

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Tony
  • 137
  • 1
  • 3
  • 11
  • As well as the answers given, note that percent must be calculated for each character, so it needs to be inside the for loop. – davidc Nov 19 '14 at 00:39

2 Answers2

0

You are trying to get the modulo (remainder) with percent which is declared/calculated as double. 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.

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0

The % modulo operator is only defined for integer types, not for floating point types. Therefore

percent % 1.0 < .5

doesn't work. You can most likely work around it in this case with:

percent - (unsigned) percent < .5

The conversion to unsigned gets rid of the decimal part. Subtracting this from the original double would lead only the decimal part which you seem to want.

Note: this isn't perfect and requires percent being positive (or some more work).

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
  • Beware that if `percent` is too large for the `unsigned` integral type then it will overflow, causing the result to be incorrect. As a general answer, [using `fmod()`](http://stackoverflow.com/questions/499939/extract-decimal-part-from-a-floating-point-number-in-c) would be a better solution. – cdhowie Nov 19 '14 at 01:07