Write a C++ program that reads in three exam grades from the input data file gra
ID: 647304 • Letter: W
Question
Write a C++ program that reads in three exam grades from the input data file grades.txt. The program should print the grades, and calculate and print the average to the output data file report.txt. Print the average to 2 decimal places. The program should also print to the output file whether the student passed or failed the course based on the average. If the average is greater than or equal to 70, the student passed. If the average is less than 70, the student failed. Finally, the program should print to the output file the letter grade the student will receive based on the average. The program should check to make sure the input data file was found. If the input file was not found, the program should terminate and not continue processing. Letter grades are determined based on the following: A = 90% or better B = 80
Explanation / Answer
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
int num1, num2, num3,avg;
ifstream infile;
file.exceptions(ifstream::failbit | ifstream::badbit);
try {
infile.open("grades.txt");
while(!infile.eof())file.get();
}
catch(ifstream::failure e)
{
cout<<"Unable to open file";
return(0);
}
infile>>num1>>num2>>num3;
infile.close();
ofstream outfile;
outfile.open("report.txt");
avg=(num1+num2+num3)/3;
cout<<"Average:"<<avg;
if(avg>=90)
cout<<"Grade: A"<<endl<<"Result: PASS"<<endl;
else if(avg>=80 && avg<=89)
cout<<"Grade: B"<<endl<<"Result: PASS"<<endl;
else if(avg>=70 && avg<=79)
cout<<"Grade: C"<<endl<<"Result: PASS"<<endl;
else if(avg>=60 && avg<=69)
cout<<"Grade: D"<<endl<<"Result: FAIL"<<endl;
else if(avg<60 && avg<=69)
cout<<"Grade: F"<<endl<<"Result: FAIL"<<endl;
outfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.