I'm pretty new to c++, and I've been trying to fix a few quirks with my program that is able to determine someone's grade by using while loops. The program needs to loop after the scores have been added up and displayed. However, in the do-while loop, the condition seems to not apply and goes straight through when i don't enter any input in the name field. After scores are added and displayed, it goes back to the enter name prompt again but no matter i put in, it never loops. I also want all input to be cleared after the grade has been displayed but i don't think i used cin.clear() correctly at the end of the do-while loop. What needs to be done?
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string name;
int score, tests = 0, total = 0; // Tests will be used to determine the amount of scores entered
double avg = 0;
bool errorflag = false;
cout << "Greater Grader \n" << endl << " This program will process test scores to provide to individuals \n with letter grades according to the following scale :" << endl;
cout << endl <<
" ******************************************* \n" // Display of grading scale
" 90 < average A \n"" 80 < average <= 90 B \n"" 67.5 < average <= 80 C \n"" 55 < average <= 67.5 D \n"" 0 < average <= 55 F \n"" *******************************************" << endl;
cout << fixed << showpoint << setprecision(1);
cout << "Type your name (press ENTER to terminate) : " << endl;
getline(cin, name);
do
{
if (cin.fail())
{
errorflag = true;
cin.clear();
}
cout << endl << "\n Enter your scores and end the list with a negative number : ";
cin >> score;
while (score >= 0)
{
tests = tests + 1;
total = (score + total);
cin >> score;
}
avg = (total / tests);
if (avg > 90) // Grading Scale
cout << endl << name << ", your grade is an A with a score of " << avg << endl;
elseif (avg > 80 && avg <= 90)
cout << endl << name << ", your grade is a B with a score of " << avg << endl;
elseif (avg > 67.5 && avg <= 80)
cout << endl << name << ", your grade is a C with a score of " << avg << endl;
elseif (avg > 55 && avg <= 67.5)
cout << endl << name << ", your grade is a D with a score of " << avg << endl;
elseif (avg >= 0 && avg <= 55)
cout << endl << name << ", your grade is an F with a score of " << avg << endl;
else
cout << endl << "You didn't enter any scores." << endl;
cout << endl <<
"=======================================================================" << endl;
cin.clear();
cout << "Type your name (press ENTER to terminate) : " << endl;
getline(cin, name);
cin.ignore(128, '\n');
} while (name.length() != 0);
cout << "No name was entered!";
cout << endl << "Goodbye";
cin.get();
return 0;
}