need advice on how to re-write below simple code by using conditions 'for' only and 'do-while' only.
I tried using 'for' on the 1st portion, but the total not able to process accurately
So far, stuck in the 'for' condition. Yet to try do-while.
Need help to point out the mistakes, and further advice for using do-while
thank you!
Original code to re-write:-
I tried using 'for' on the 1st portion, but the total not able to process accurately
So far, stuck in the 'for' condition. Yet to try do-while.
Need help to point out the mistakes, and further advice for using do-while
thank you!
for (int grade = 0; grade != -1; cout << "Enter grade or -1 to quit: ") gradeCounter++; cin >> grade; total += grade;
Original code to re-write:-
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int grade;
int gradeCounter;
int total;
double average;
gradeCounter = 0;
total = 0;
cout << "Enter grade or -1 to quit: ";
cin >> grade;
while (grade != -1)
{
total = total + grade;
gradeCounter = gradeCounter + 1;
cout << "Enter grade or -1 to quit: ";
cin >> grade;
}
if (gradeCounter != 0)
{
average = static_cast < double >(total)/gradeCounter;
cout << "\nTotal of all " << gradeCounter << " grades entered is " << total << endl;
// out << "Class average is " << setprecision(3) << average << endl;
cout << "Class average is " << setprecision(2) << fixed << average << endl;
cin.sync();
cin.get();
}
else
cout << "No grades were entered" << endl;
cin.sync();
cin.get();
}