Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Newbie problem that I just can't solve

This topic is 5512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

As part of a project in a computer class I am taking (EAST Lab), I am programming a math calculation program to do useful mathematical calculations for a teacher or student. This is not the final version, just a DOS-console prototype. However, I have run into a problem I''ve never come across before. I am relatively new to coding, and although I know a bit about it, I''m still failing pretty badly in a few spots, apparently. The problem I''m having comes in the averaging function. I can average integers, but it will not give me a decimal, so I tried changing the ints to floats instead. This is really just a shot in the dark...I''ve never done this before. Anyway, I seem to be having a very ugly problem that is giving me 4 errors, every time I try to compile. Here''s the function: int AverageNums() { float fNumstoAvg = 0.0f; float i = 1.0f; system("cls"); cout << "Average -- Average some numbers."; cout << "\n\nHow many numbers would you like to average? "; cin >> fNumstoAvg; if (fNumstoAvg > 255.0f) { cout << "\nYou cannot average any more numbers than 255.\n\n"; system("cls"); return 0; } cout << "\n\n"; float fNumsIAvg[255]; for (i=1.0f;i <= fNumstoAvg; i++) { cout << "What is number " << i << "? "; cin >> fNumsIAvg; } // end for float fSum = 0.0f; for (i=1.0f;i <= fNumstoAvg; i++) { fSum = fSum + fNumsIAvg[i]; } float fAveraged = 0.0f; fAveraged = fSum / fNumstoAvg; cout << "\n\n"; cout << "Averaged together, the total is " << fAveraged << ".\n\n"; system("pause"); system("cls"); return 0; } I am aware of a couple of stupid mess-ups, such as having an int function return 0 for no apparent reason, when I could just make it a void function and have it simply return. However, the problem seems to be in the addition for-loop, which contains "fSum = fSum + fNumsIAvg[i];". Also, a problem seems to exist in the "cin >> fNumsIAvg[i];" line...the first two errors relate to this line, and the last two relate to the line in the above paragraph. The errors are: 1. error C2108: subscript is not of integral type 2. error C2679: binary ''>>'' : no operator defined which takes a right-hand operand of type ''float *'' (or there is no acceptable conversion) 3. error C2108: subscript is not of integral type 4. error C2111: pointer addition requires integral operand Anyone have any ideas what my lacking mind is missing?

##### Share on other sites
[qoute]
error C2108: subscript is not of integral type

if you're going to use i as a subscript (array) it can't be a float... or you'll have to cast it as an int (array[(int)i]) I don't recommend that

quote:

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'float *' (or there is no acceptable conversion)

you can't use cin with a float... sorry

I'd suggest going back to integers... you only really need to make fAveraged a float... you could also make fSum a float so you don't have to worry about integer division

[edited by - tempuself on November 11, 2003 10:40:12 AM]

##### Share on other sites
You cannot have a float in a for loop. It must be an integer. Change i to an int.

##### Share on other sites
Most \$19.99 calculators will do all of that already.

##### Share on other sites
well, AverageNum() returns int ...

##### Share on other sites
quote:
Original post by Anonymous Poster
You cannot have a float in a for loop.

Of course you can.

for (float loop = 9.99f; loop > 0.01f; loop -= 0.01f){  // bla bla}

##### Share on other sites
quote:
Original post by TempusElf
quote:

error C2679: binary ''>>'' : no operator defined which takes a right-hand operand of type ''float *'' (or there is no acceptable conversion)

you can''t use cin with a float... sorry

Nonsense. Of course you can use cin with a float. Not, however, with a float pointer ...

##### Share on other sites
Almost forgot, this should work:

int AverageNums(){  int fNumstoAvg = 0;  int i = 0;  system("cls");  cout << "Average -- Average some numbers.";  cout << "\n\nHow many numbers would you like to average? ";  cin >> fNumstoAvg;  if (fNumstoAvg > 255)  {    cout << "\nYou cannot average any more numbers than 255.\n\n";    system("cls");    return 0;  }  cout << "\n\n";  float fNumsIAvg[255];  for (i=0; i < fNumstoAvg; i++)  {    cout << "What is number " << i << "? ";    cin >> fNumsIAvg[i];  } // end for  float fSum = 0.0f;  for (i= 0; i < fNumstoAvg; i++)  {    fSum = fSum + fNumsIAvg[i];  }  float fAveraged = 0.0f;  fAveraged = fSum / fNumstoAvg;  cout << "\n\n";  cout << "Averaged together, the total is " << fAveraged << ".\n\n";  system("pause");  system("cls");  return 0;}

[edited by - EL on November 11, 2003 4:02:25 PM]

##### Share on other sites
Here's a revamped version because I'm bored: (also, use [ source ] tags for big code snippets)
void average(){   long total=0;   int numberOfNumbers=0;   cout<<"How many numbers would you like to average? ";   cin>>numberOfNumbers;   for(int i=0;i<numberOfNumbers;i++)   {      cout<<"Enter number "<<i<<": ";      int temp=0;      cin>>temp;      total+=temp;   }   float result=((float)total/(float)numberOfNumbers);   cout<<"result: "<<result;}

[edited by - brassfish89 on November 11, 2003 10:23:07 PM]

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013478
×