I am having trouble with my output lining up and all my averagesneed to be to tw
ID: 3614413 • Letter: I
Question
I am having trouble with my output lining up and all my averagesneed to be to two decimal places. I know I need a“setprecision(2)” somewhere, just don’t knowwhere. Thanks for any help!
INPUT
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
PROGRAM
//This program reads input of students names and grades,calculates them, then
//outputs test avgerage and class average of the students
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void CalculateAverage (ifstream& Infile, string& SName,double& SAvg,
ofstream& Outfile, int NumGrades);
char CalculateGrade (double Avg);
int main()
{
ifstreamInFile;
ofstream OutFile;
stringName;
int NumStudents = 0;
intNG;
double Avg = 0.0, ClassTotal = 0.0;
NG = 5;
InFile.open("e:\chap7inputGRADES.txt");
if (!InFile)
{
cout << "Error opening theInput File!" << endl;
return 1;
}
OutFile.open("e:\chap7outputAvgGRADE.txt");
if (!OutFile)
{
cout << "Error opening the OutputFile!" << endl;
return 1;
}
OutFile <<"Student Name Test 1 Test 2 Test3 Test 4 Test 5 Average Grade"
<< endl;
while (!InFile.eof())
{
CalculateAverage(InFile, Name, Avg, OutFile,NG);
OutFile << " " << Avg<< CalculateGrade(Avg) << endl;
NumStudents++;
ClassTotal = ClassTotal +Avg;
}
OutFile <<endl << "Class Average = "<<ClassTotal / NumStudents << endl<<endl;
InFile.close();
OutFile.close();
system("pause");
return 0;
}
void CalculateAverage (ifstream& Infile, string& SName,double& SAvg,
ofstream& Outfile, int NG)
{
double Score, Total = 0.0;
int x;
Infile >> SName;
Outfile << " " << SName;
for (x=1; x <= NG; x++)
{
Infile >> Score;
Total = Total + Score;
Outfile << Score << " ";
}
SAvg = Total / NG;
Outfile << endl;
}
char CalculateGrade (double SAvg)
{
switch ((int)SAvg / 10)
{
case 10:
case 9: return 'A';
break;
case 8: return 'B';
break;
case 7: return 'C';
break;
case 6: return 'D';
break;
default: return 'F';
}
}
OUTPUT
Student Name Test 1 Test 2 Test 3 Test4 Test 5 Average Grade
Johnson85 83 77 91 76
82.4B
Aniston80 90 95 93 48
81.2B
Cooper78 81 11 90 73
66.6D
Gupta92 83 30 69 87
72.2C
Blair23 45 96 38 59
52.2F
Clark60 85 45 39 67
59.2F
Kennedy77 31 52 74 83
63.4D
Bronson93 94 89 77 97
90A
Sunny79 85 28 93 82
73.4C
Smith85 72 49 75 63
68.8D
Class Average = 70.94
Explanation / Answer
please rate - thanks //This program reads input of students names and grades, calculatesthem, then //outputs test avgerage and class average of the students #include #include #include #include using namespace std; void CalculateAverage (ifstream& Infile, string& SName,double& SAvg, ofstream& Outfile, int NumGrades); char CalculateGrade (double Avg); int main() { ifstream InFile; ofstream OutFile; stringName; int NumStudents = 0; intNG; double Avg = 0.0, ClassTotal = 0.0; NG = 5; InFile.open("e:\chap7inputGRADES.txt"); if (!InFile) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.