c语言文件无限循环的,在c中读取文件时fstream无限循环

我在学校的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;

}

如果有人能向我解释为什么我会继续获得无限循环,我会非常感激.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值