for the original code, it's within a loop
for example
while ( grade != -1 ) {
total = total + grade;
gradeCounter = gradeCounter + 1
.... skip the rest
}
in the book, just like what you said above
Quote
++a = increment a by 1, then use the new value of a in the expression in which a resides
a++ = use the current value of a in the expression in which a resides, then increment a by 1
since the loop is going to check the value, if the value is -1, then the loop stops, otherwise, the counter will increase by 1 each time a new value enters.
and if this is the case, in another word, gradeCounter is postincrement (a++)? am i correct?
but when i looked over the book, different examples shows different increment operator, can be a++, ++a, so i bring up here and ask for discussion :) thanks
for the original code, the whole program is quoted below
// This is a C++ program
// Welcome to another program written by me John Wong
// Okay, let's go to the program then
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
// declare and define variables
int grade, total, gradeCounter;
float average;
// initialize sets values
total = 0;
gradeCounter = 0;
// print some messages first
cout << "Welcome to simple average program!\n" << endl;
cout << "Please enter each grade values and to terminate the program to print the result, please type -1 into the grade value.\n" << endl;
// now, let us begin our program
cin.get();
cout << "Enter a grade, -1 to end: ";
cin >> grade;
cin.get();
while ( grade != -1 ) {
total = total + grade;
gradeCounter = gradeCounter + 1;
cout << "Enter a grade, -1 to end: ";
cin >> grade;
}
cin.get();
// terminate the program
if ( gradeCounter != 0 ) {
average = static_cast< float > (total) / gradeCounter;
cout << "Average is " << setprecision ( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << average << endl;
}
else
cout << "No grade were entered" << endl;
cin.get();
// end the entire program
return 0;
}