While Statement not counting first number entered but counting all others...

My C++ program is supposed to sum and display the average score of students in a class and display the total number of scores entered. The problem I'm having is the program totals the last four entries and then divides by 5 (the total number of entries) but the first entry (100) is not being included in the summation (Numbers 100, 80, 95, 82, 79, which calculates to 87.2). My program calculates number 67, so I assume 100 is not being included. Negative 1 is set as the sentinel.

I also can't recall how to set the program to display the decimal in 87.2. Any help would be appreciated.

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>

using namespace std; 

int main ()
{
    double score = 0.0;
    int numScore = 0; //counter
    double avgScore = 0.0; //accumulator
    double totScore = 0.0;

    cout << "Enter the student's score [-1 to stop]: ";
    cin >> score;
    
    while (score != -1)
    {
          numScore = numScore + 1;
          
          cout << "Enter the student's score [-1 to stop]: ";
          cin >> score;
          
          avgScore = avgScore + score;
          totScore = avgScore / numScore;
    }
     
    cout << "Number of scores entered: " << numScore << endl;
    cout << "Average score: " << totScore << endl;
    
    system ("pause");
    return 0;
}
I think that the variable with name totScore should keep indeed a total score, not the average as in your code.:)


1
2
3
4
5
6
7
8
9
10
11
12
13
    while ( true )
    {
          cout << "Enter the student's score [-1 to stop]: ";
          cin >> score;
          
          if ( score == -1 ) break;
          
          totScore += score;
          numScore++;
    }
     
    cout << "Number of scores entered: " << numScore << endl;
    cout << "Average score: " << totScore / numScore << endl;
You are overwriting the first score variable that is called outside the loop without including it in your calculations. just delete lines 12 and 13 and it should run just like you want.

edit;
Vlad is right about keeping the calculation for averages outside of the loop, it doesn't have to be in there, and the more wasted coding inside of a loop the slower the program will run. You wouldn't notice a difference here but in larger/longer coding you might start to see a time lag. Try to keep your loops clean, it is good practice for when you're coding huge programs.
Last edited on
Thank you so much, Vlad! You are the man!! :-)

We haven't learned about the "++" yet, so I hope my professor won't count this against me?? Can you explain what "numScore++" means? I also took your advice and got rid of the "totScore" variable. It was unnecessary and had nothing to do with the average as you so kindly pointed out...
As a standalone expression
numScore++ is equivalent to numScore = numScore + 1;
Got it, Vlad. I will rewrite it as indicated just to be safe. And thanks again for your quick reply. You are always so helpful! :-)

Newbieg, thank you for your response too. I removed lines 12 and 13 as you suggested, but the program recorded "6 entries" instead of 5, and the average score displayed 72.5 instead of 87.2. Hmm...
Heh, sorry, I was trying to solve the issue without running it through my compiler.
So the problem was the order that the calculations were run, the way it was set up before still allowed avgScore (which you are changing to totScore) to take -1 as part of the averages. The place where Vlad breaks the loop solves that problem. Sorry, I should have run it through my compiler before making suggestions.
Last edited on
No problem, Newbieg. I really appreciate your input and hope you'll participate again if I post another question, which I'm sure will be soon!
Topic archived. No new replies allowed.