O
oLgAa25
I have the following question,
I am trying to read from a file and output to another file.
But I am not getting exactly the expected output.
Can anyone help please!
using namespace std;
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage);
int calculateGrade(double grade);
int main()
{
string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double studentAverage = 0;
double totalAverage = 0; //To add the average of all student
averages
char grade;
double test1, test2, test3, test4, test5;
ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable
inFile.open("c:\\studentFile.txt");
outFile.open("c:\\Student_out.txt");
cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;
outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;
if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}
while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);
inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;
calculateAverage(test1, test2, test3, test4, test5,
studentAverage);
grade = calculateGrade(studentAverage);
cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
totalAverage = totalAverage + studentAverage;
numberOfStudents++;
classAverage = totalAverage / numberOfStudents;
}
outFile << endl << setprecision(2)<< "Class average is:" <<
classAverage << endl;
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}
//function to calculate the average
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage)
{
studentAverage = static_cast<double>(test1 + test2 + test3 + test4
+ test5) / 5.0;
}
int calculateGrade(double studentAverage)
{
char grade;
if (studentAverage <= 100 && studentAverage >= 90)
grade = 'A';
else if (studentAverage < 90 && studentAverage >= 80)
grade = 'B';
else if (studentAverage < 80 && studentAverage >= 70)
grade = 'C';
else if (studentAverage < 70 && studentAverage >= 60)
grade = 'D';
else if (studentAverage < 60 && studentAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;
return grade;
}
I am trying to read from a file and output to another file.
But I am not getting exactly the expected output.
Can anyone help please!
using namespace std;
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage);
int calculateGrade(double grade);
int main()
{
string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double studentAverage = 0;
double totalAverage = 0; //To add the average of all student
averages
char grade;
double test1, test2, test3, test4, test5;
ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable
inFile.open("c:\\studentFile.txt");
outFile.open("c:\\Student_out.txt");
cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;
outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;
if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}
while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);
inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;
calculateAverage(test1, test2, test3, test4, test5,
studentAverage);
grade = calculateGrade(studentAverage);
cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
totalAverage = totalAverage + studentAverage;
numberOfStudents++;
classAverage = totalAverage / numberOfStudents;
}
outFile << endl << setprecision(2)<< "Class average is:" <<
classAverage << endl;
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}
//function to calculate the average
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage)
{
studentAverage = static_cast<double>(test1 + test2 + test3 + test4
+ test5) / 5.0;
}
int calculateGrade(double studentAverage)
{
char grade;
if (studentAverage <= 100 && studentAverage >= 90)
grade = 'A';
else if (studentAverage < 90 && studentAverage >= 80)
grade = 'B';
else if (studentAverage < 80 && studentAverage >= 70)
grade = 'C';
else if (studentAverage < 70 && studentAverage >= 60)
grade = 'D';
else if (studentAverage < 60 && studentAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;
return grade;
}