I'm working on some code for a school project that involves outputting the average GPA of a group of male and female students. The total GPA (before dividing to get the average) needs to be rounded as each student's GPA is added in order to get the correct output. However, the only way I know to round a double is to use the formatting command setprecision, and that only works if you're outputting the data, and I won't be outputting the data until the average is calculated. (the input data is coming from an input file, the variable for which is "in.")
Here's the relevant section of code:
//while not at end of input file, continue adding data to GPA totals.
while (!in.eof())
{
in >> gender; //input data from file
in >> student;
switch (gender) { //switch to code for male or female
case 'f':
case 'F':
ftotal += student; //add student's GPA to total
fctr++; //add 1 to counter
break;
case 'm':
case 'M':
mtotal += student; //add student's GPA to total
mctr++; //add 1 to counter
break;
default:
cout << "Input File Error. Please check input." << endl;
}
}
I need some sort of command to round mtotal and ftotal to 2 decimal places before the loop restarts each time. Any help would be greatly appreciated.