Friday, July 31, 2009

C ++ help anyone please!!! i am getting this error: Error 1error C2108: subscript is not of integral type?

first of all i don't quite undurstand the problem....this is the problem:


The Federal Reserve wants a program that can give the average interest rate on mortgages using data from four nationally ranked banks. Your tasks are to create a program that calculates the average of values stored in a one-dimensional double array named rates. The program should display the average rate on the screen.


Use the following data to:


Bank of America 6.2%, RBC Centura 7.1%, BB%26amp;T 5.9%, Federal Credit Union 6.0%





this is my code:


#include %26lt;iostream%26gt;





using namespace std;





int main()


{


//declare array


double total = 0;


double average = 0;


double rate[4] = {6.2, 7.1, 5.9, 6.0};








// calculate sum


for (double x = 0; x %26lt; 4; x = x + 1)


total = total + rate[x];


//end for





//calculate and display average


average = total / 4;





cout %26lt;%26lt; "Average: " %26lt;%26lt; average %26lt;%26lt;endl;


return 0;


}/





is it right? what i am doing wrong?i am just a beginner help me please!!

C ++ help anyone please!!! i am getting this error: Error 1error C2108: subscript is not of integral type?
X is the subscript, so x has to be an integer. Computers treat integers, floats and doubles differently so NEVER use a float or a double as an integer unless the computer has been treating you horribly -- and even then, obviously, you will only be torturing it for a few nanoseconds until the compiler catches you.





Change it to:


for (int x=0; x%26lt;4; x=x+1)
Reply:You cannot use a non-integral datatype as a subscript; you need to use the int datatype. For example:


// calculate sum


for (int x = 0; x %26lt; 4; x = x + 1)


total = total + rate[x];


//end for
Reply:check this out. i have the book in my hands right now. want me to get back to u? u can trust me dude i have the book in my hands .








(im a computer nerd)
Reply:Double types are not integer. They're floating point. You can't use a floating point number as a subscript.





Hope that helps.
Reply:I'll tell you the real answer without me telling you I'm a computer nerd. Change this line from double to int.





for (int x = 0; x %26lt; 4; x = x + 1)


No comments:

Post a Comment