
March 19th, 2013, 05:41 PM
#1
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, 05:56 PM
#2
Re: have en error: expression must have integral or enumtype
Originally Posted by f0rumh4x
What is wrong with > total += rates[i][n];
Array indexes must be an integral type.

March 19th, 2013, 05:56 PM
#3
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, 06:08 PM
#4
Re: have en error: expression must have integral or enumtype
Thank you very much...that worked. Now to keep studying.

March 20th, 2013, 01:43 AM
#5
Re: have en error: expression must have integral or enumtype
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.
Last edited by nuzzle; March 20th, 2013 at 01:46 AM.

March 21st, 2013, 12:54 PM
#6
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, 06:09 PM
#7
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, 06:22 PM
#8
Re: have en error: expression must have integral or enumtype
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, nonterminating binary floating point number. So you have a nonrepeating decimal fraction being translated into a repeating, nonterminating binary floating point number. Those bits on the far end of the binary number start to propagate, causing roundoff 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/floatingpterrs.html
http://www.parashift.com/c++faq/flo...intarith.html
http://www.parashift.com/c++faq/flo...ntarith2.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
Last edited by Paul McKenzie; March 21st, 2013 at 06:31 PM.

March 22nd, 2013, 06:20 AM
#9
Re: have en error: expression must have integral or enumtype
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/E1995701/..._goldberg.html.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
