#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
char calculate_grade(double avg);
char calculate_grade(double avg) {
if (avg >= 75) { return 'A'; }
else if (avg >= 65) { return 'B'; }
else if (avg >= 55) { return 'C'; }
else if (avg >= 40) { return 'D'; }
else { return 'E'; }
}
int main() {
ifstream infile("Data.txt");
ofstream outfile("ResultData.out");
string name;
double score;
outfile << left << setw(10) << "Name"
<<right << setw(10) << "score1"
<<right << setw(10) << "score2"
<<right << setw(10) << "score3"
<<right << setw(10) << "avg"
<<right << setw(10) << "grade" << endl;
while (infile >> score1 >> score2 >> score3 >> name) {
double avg = (score1 + score2 + score3) / 3.0;
char grade = calculate_grade(avg);
outfile << setw(10) << name << " "
<< setw(10) << score1 << " "
<< setw(10) << score2 << " "
<< setw(10) << score3 << " "
<< setw(10) << avg << " "
<< setw(10) << grade << " " << endl;
}
infile.close();
outfile.close();
return 0;
}
I want to use the content of a text file to let C++ help me calculate the average score and grade of the students and then output it in the text file, but I can't get it. i don't how to do it or anywhere is wrong
I'm fairly new to C++ so I'm sorry if it does have a simple answer. But anyway, I've been debugging this for few hour and I looked online for few time also
Content of file matters. And you didn't even check if file is opened. There will be no errors unless you write code to process them.
Also there is a problem with input. name is a string.
infile >> namewould eat away whole record.