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.
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.
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...
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.