counter problem

• 03-07-2002
Unregistered
counter problem
hello

i write a program like this:#include <iostream.h>
#include <iomanip.h>
int main ()
{
int score,
totalscore,
counter,
execute;
double avgscore;
execute = 999;
counter = 0;
totalscore = 0;
cout << "Intruction: \nEnter score range from 0 - 100 \nAfter that enter 999 for average score calculation " << endl;

cout << "Now enter a score: " ;
cin >> score;
do
{
if ( score < 0 || score > 100 )
cout << "Error, input again " << endl;
cout << "Enter a score: " ;
cin >> score;

}while ( score < 0 || score > 100 );

while ( score != execute )
{

totalscore = totalscore + score;
counter++;
cout << "Enter next score: ";
cin >> score;

avgscore = totalscore/counter;
cout << "The average score is: " << avgscore << endl;

}

return 0;
}

// the problem is when enter score : 80, 90, 105, 70, 999
valid score should be: 80, 90, 70
valid count should be: 3
its good to display error at 105, however the "counter" count it as an entry, so the total score devide more than valid counter, the average score will be wrong.

any one can help to fix this? I try too many way, but won't work
hopefully to solve it here.
• 03-07-2002
swoopy
Try this.
Code:

```#include <iostream.h> #include <iomanip.h> int main () { int score, totalscore, counter, execute; double avgscore; execute = 999; counter = 0; totalscore = 0; cout << "Intruction: \nEnter score range from 0 - 100 \nAfter that enter 999 for average score calculation " << endl; cout << "Now enter a score: " ; cin >> score; do {   while ( score < 0 || score > 100 )   {       cout << "Error, input again " << endl;       cout << "Enter a score: " ;       cin >> score;   };   totalscore += score;   counter++;   cout << "Enter next score: ";   cin >> score; } while ( score != execute ); avgscore = totalscore/counter; cout << "The average score is: " << avgscore << endl; return 0; }```
• 03-09-2002
Unregistered
thank you buddy

it works!!!!!!!

:) :)

I really mess up side down right?

thank you again swoopy
• 03-09-2002
swoopy
>I really mess up side down right?
Exactly. Of course often there's several different ways to get the job accomplished.