我在学校的CS101课堂上使用C语言.我有一个程序必须以这种格式读取文件:
Ramirez, Manny
1572838992 a 4 b 5 x 4 a 7 c 3 c 4 *
Kemp, Matt
3337474858 a 4 b 4 b 4 a 4 *
它应该读取文件,计算每个学生的GPA并输出学生的姓名,ID,单位和GPA.我已经尝试了一段时间不同的事情,而且我一直在无限循环中来回走动.我得到了一位朋友的帮助,他让我加入了一些东西.
我已经尝试在while(!eof)循环中改变循环的条件这么多次我无法统计它们.每当我查看在线做什么时,我得到的建议就是不要使用while!eof,因为它有导致无限循环的倾向.好吧,我现在知道了,但我的教授希望我们这样做.
这是我的代码:
#include
#include
#include
#include
#include
using namespace std;
int main() {
char grade;
int units=0, studentsGrade=0, studentUnits=0;
int unitTotal=0;
float gradeTotal=0, gpa = 0;
string inputName, outputName, fullName;
long ID;
ifstream inputFile;
ofstream outputFile;
cout << "Please enter the input filename: ";
cin >> inputName;
cout << "Please enter the output filename: ";
cin >> outputName;
inputFile.open(inputName.c_str());
if (inputFile.fail())
cout << "Bad input file name." << endl;
else {
outputFile.open(outputName.c_str());
outputFile << left << setw(25) << "Name" << setw(15) << "ID" << setw(15)
<< "Units" << setw(15) << "GPA" << endl;
outputFile << "--------------------------------------------------------------------------------" << endl;
cout << left << setw(25) << "Name" << setw(15) << "ID" << setw(15)
<< "Units" << setw(15) << "GPA" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
getline(inputFile, fullName);
while (!inputFile.eof()) {
gpa = 0;
unitTotal = 0;
gradeTotal = 0;
inputFile >> ID;
outputFile << setw(25) << fullName;
outputFile << setw(15) << ID;
cout << setw(25) << fullName << setw(15) << ID;
string line;
getline(inputFile,line);
istringstream iss(line);
while (!iss.eof()) {
units = 0;
iss >> grade >> units;
if (grade == '*')
break;
if (units > 0 && units <=5 && (grade == 'a' || grade == 'A')) {
gradeTotal += 4 * units;
studentsGrade += 4 * units;
unitTotal += units;
studentUnits += units;}
else if (units > 0 && units <=5 && (grade == 'b' || grade == 'B')) {
gradeTotal += 3 * units;
studentsGrade += 3 * units;
unitTotal += units;
studentUnits += units; }
else if (units > 0 && units <=5 && (grade == 'c' || grade == 'C')) {
gradeTotal += 2 * units;
studentsGrade += 2 * units;
unitTotal += units;
studentUnits += units; }
else if (units > 0 && units <=5 && (grade == 'd' || grade == 'D')) {
gradeTotal += 1 * units;
studentsGrade += 1 * units;
unitTotal += units;
studentUnits += units; }
else if (units > 0 && units <=5 && (grade == 'f' || grade == 'F')) {
unitTotal += units;
studentUnits += units; }
else if (grade == '*') {
unitTotal += 0;}
else {
unitTotal += 0; }
}
gpa = (float)gradeTotal / unitTotal;
outputFile << fixed << showpoint;
outputFile << setw(15) << unitTotal << setw(15) << setprecision(2) << gpa << endl;
cout << fixed << showpoint;
cout << setw(15) << unitTotal << setw(15) << setprecision(2) << gpa << endl;
getline(inputFile,fullName);
}
outputFile << "The GPA for all students is " << setprecision(2) << (float)studentsGrade / studentUnits;
cout << "The GPA for all students is " << setprecision(2) << (float)studentsGrade / studentUnits << endl;
}
inputFile.close();
outputFile.close();
cout << endl;
return 0;
}
如果有人能向我解释为什么我会继续获得无限循环,我会非常感激.