CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2013
    Location
    Gastonia, NC
    Posts
    11

    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.

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: have en error: expression must have integral or enumtype

    Quote Originally Posted by f0rumh4x View Post
    What is wrong with --> total += rates[i][n];
    Array indexes must be an integral type.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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.

  4. #4
    Join Date
    Jan 2013
    Location
    Gastonia, NC
    Posts
    11

    Re: have en error: expression must have integral or enumtype

    Thank you very much...that worked. Now to keep studying.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: have en error: expression must have integral or enumtype

    Quote Originally Posted by f0rumh4x View Post
    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 12:46 AM.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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)

  7. #7
    Join Date
    Jan 2013
    Location
    Gastonia, NC
    Posts
    11

    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: have en error: expression must have integral or enumtype

    Quote Originally Posted by f0rumh4x View Post
    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
    Last edited by Paul McKenzie; March 21st, 2013 at 05:31 PM.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: have en error: expression must have integral or enumtype

    Quote Originally Posted by f0rumh4x View Post
    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.
    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
  •  





Click Here to Expand Forum to Full Width

Featured