have en error: expression must have integral or enumtype

• March 19th, 2013, 04:41 PM
f0rumh4x
have en error: expression must have integral or enumtype
Code:

```#include <iostream> #include <iomanip> using namespace std; //chose to use #define since columns/rows are a constant in the program #define x 5 #define y 3 int main() {         //declare variables         double i, n, total = 0.0;         float average;         //declare array and set values stored in it         double rates[5][3] = {{3.4, 56.7, 8.99},                                                   {11.23, 4.67, 85.4},                                                   {34.6, 2.4, 9.0},                                                   {6.3, 8.0, 4.1},                                                   {4.0, 2.0, 3.5}};         for (i = 0; i < x; i++)                 for (n = 0; n < y; n++)                         total += rates[i][n];         //end for         //calculate the average of values stored in array         average = (float)total/(float) x*y;         //display average with two decimal places         cout << fixed << setprecision(2);         cout << "The average equals: " << average << endl;         cin.get();         return 0; }        //end of main function```
What is wrong with --> total += rates[i][n];
Thanks in advance for taking a look at it.
• March 19th, 2013, 04:56 PM
nuzzle
Re: have en error: expression must have integral or enumtype
Quote:

Originally Posted by f0rumh4x
What is wrong with --> total += rates[i][n];

Array indexes must be an integral type.
• March 19th, 2013, 04:56 PM
Philip Nicoletti
Re: have en error: expression must have integral or enumtype
The variables : "i" and "n" should be of integral type (such as int), not double.
• March 19th, 2013, 05:08 PM
f0rumh4x
Re: have en error: expression must have integral or enumtype
Thank you very much...that worked. Now to keep studying.
• March 20th, 2013, 12:43 AM
nuzzle
Re: have en error: expression must have integral or enumtype
Quote:

Originally Posted by f0rumh4x
Now to keep studying.

Note that loop iteration variables preferrably should be of integral type. If floating points are used small rounding errors may accumulate and become substantial.

Also be careful with literals; 1 is an integer whereas 1.0 is a floating point.
• March 21st, 2013, 11:54 AM
OReubens
Re: have en error: expression must have integral or enumtype
correction:

floating point errors are only a concern when using decimal parts. float and double can perfectly hold integer values without any form of precision loss in so far as you don't try to store more integer bits than the floating point value has (implicit or explicit) enough mantissa bits.

So you can store a short in a float (24bits mantissa) and a long in a double (53 bits mantissa) and retrieve them back without rounding issues. (and actually still have room to spare)
• March 21st, 2013, 05:09 PM
f0rumh4x
Re: have en error: expression must have integral or enumtype
Is there one specific resource that would be a good reference for understanding floating point better that I can be directed to. I am always looking for more material... I can search multiple resources and get there, but was wondering if any one source is more recommended than another? Again thank you all for the information. It helps more than being given code that I haven't written myself. I think I learn the most from correcting errors I make.
• March 21st, 2013, 05:22 PM
Paul McKenzie
Re: have en error: expression must have integral or enumtype
Quote:

Originally Posted by f0rumh4x
Is there one specific resource that would be a good reference for understanding floating point better that I can be directed to.

Well, all you would need to know in a general sense is that floating point arithmetic is not exact, unless you're adding/subtracting negative powers of 2.

Here is a good example of a loop that uses floating point, where you can get in trouble:
Code:

```for (double d = 0.0; d < 1.0; d += 0.01) { }```
If that loop is allowed to be executed, how many times will that loop execute? If you use an answer based on adding exactly 0.01 to d, then you may be right -- or you may be wrong. Not only that, you may be right if certain compiler switches are used to build the program, but for another set of compiler switches, the very same code produces a different answer.

The issue is that decimal 0.01 is not an exact binary floating point number. It is a repeating, non-terminating binary floating point number. So you have a non-repeating decimal fraction being translated into a repeating, non-terminating binary floating point number. Those bits on the far end of the binary number start to propagate, causing round-off error. So when you add 0.01 to d, the value of "d" may be off just enough to remove an iteration from, or add an iteration to the loop.

http://www.parashift.com/c++-faq/floating-pt-errs.html
http://www.parashift.com/c++-faq/flo...int-arith.html
http://www.parashift.com/c++-faq/flo...nt-arith2.html

Also, there is a security site that has identified code like the loop above as a security risk. Since it is indeterminate as to the number of times the loop will execute, the code is considered flawed.

The bottom line is that for loop indices, always strive to use integer loop indices so you know for sure how many times the loop is to be executed. To make sure of this, it may require a simple change (divide by 10 or 100, 1000, etc. within the loop), or the solution may be more elaborate. But whatever the solution, the loop indices are to be integer to ensure how many times a loop will execute.

Regards,

Paul McKenzie
• March 22nd, 2013, 05:20 AM
D_Drmmr
Re: have en error: expression must have integral or enumtype
Quote:

Originally Posted by f0rumh4x
Is there one specific resource that would be a good reference for understanding floating point better that I can be directed to.

If you really want to get into this stuff, have a look at http://docs.oracle.com/cd/E19957-01/..._goldberg.html.