# Having trouble with averaging

• 09-25-2005
live_2_dream
Having trouble with averaging
I am working on a program and it is not averaging properly. I cannot figure out how to fix this.

In short, the program is set up to add in grades from 0 to 100; any grades over 100 are invalid, and a grade entry of 999 should stop the loop and average the grades.

The problem I am having is that the program is counting the invalid grades, which is making the average calculation incorrect.

Here is the program:

Code:

```#include <iostream> using namespace std; int main() {     int x;     double grade, totgrade, count;         totgrade = 0;     count = 0;         cout << "Please enter grade:  ";         cin  >> grade;         do         {                    if (grade > 100)                   cout << "\nInvalid grade;  ";                   if (grade <= 100)                   totgrade = totgrade + grade;                   cout << "\nPlease enter next grade:  ";                   cin  >> grade;                   count++;                   }                   while (grade != 999);         do         {                     if (grade == 999)                     cout << "The average of these grades is:  ";                     cout << totgrade / count;                     {                         break;                         }                         }                         while (grade == 999);                         cin >> x;            return 0;     return EXIT_SUCCESS; }```

// TEST
//
//
//
//
//
//The average of these grades is: 40

• 09-25-2005
Salem
Either subtract 999 before you average, or break out of the loop before you add it.
• 09-25-2005
live_2_dream
The problem isn't occuring with the 999 entry.

When I enter an invalid entry to test the program, it is adding the invalid entry (101 in this case) to the total number of entries. It is not adding the 101 to the total grades.

As this part of the program is within the loop, I am unsure how to correct it.

Any idea?
• 09-25-2005
digitaltsai
You are doing count++ regardless of whether the grade is valid or not.
• 09-25-2005
Decrypt
count is incremented now matter what grade is. You only want to increase it when the grade entered is valid.

Decrypt

Edit: oops, someone else posted while I was typing.
• 09-25-2005
live_2_dream
I tried moving the count++ up 4 lines so that it was before the totgrade statement but it didn't solve the problem. I also tried entering count = count -1 after the invalid if statement and that really messed up the calculation.

I am new to C++ and don't know what else to try.
• 09-25-2005
live_2_dream
I also tried reversing the order of the if statements...that didn't work either.

Shouldn't there be a way to fix this within the loop?
• 09-25-2005
digitaltsai
You want to include the count++ in the IF where the grade is less than 101. You would use brackets.

http://www.cprogramming.com/tutorial/lesson2.html
• 09-25-2005
live_2_dream
Brackets ..... :rolleyes:

OMG I knew it would be something that simple!! You have no idea how much time I have spent on this. :o

Thanks so much!!