Reputation: 17
I can only use a while Loop. I am to prompt the user for their first and last name and the number of exams taken. Then i am to use a while loop to sum and average the exams. Noting that each test score is a 100.
I am getting into an infinite loop, but i don't know what value would make this loop go into false besides 0.
Here is my Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string fname, lname;
int tests;
int test = 100;
double test_avg;
cout << "Please enter your first and last name." << endl;
cin >> fname >> lname;
cout << "Please enter the number of exams you have taken." << endl;
cin >> tests;
while( tests > 1 )
{
test_avg = (test * tests) / tests;
cout << setprecision(1) << showpoint << fixed;
cout << fname << ' ' << lname << ' ' << test_avg << "%" << endl;
tests = 0;
}
return 0;
}
Upvotes: 0
Views: 80
Reputation: 3325
"...i am to use a while loop and average the exams"
If you do not ask a person about the score of each exam, the avg will always be 100, which I suppose is the maximum score.
For a loop, always ask yourself - when should the loop finish? I'd say, when a person will stop entering the results.
You can organise input as follows:
int scores=0;
int cnt = 0;
while (true) {
cout << "Please enter the scores of the exams you have taken. (-1 to finish)" << endl;
cin >> score; // todo: check correctness - a number, 0 <= score <= 100, etc.
if (score == -1)
break;
cnt++;
scores += score;
}
double test_avg = 0.0;
if (cnt > 0)
test_avg = double(scores) / cnt;
Upvotes: 1