C++ 无限循环误差。输出文件中没有任何内容
我的代码在第二个while循环中不断中断。有人能帮忙吗。 文件输入是学生的姓名,然后是ID,然后是所述学生的成绩。我已经为此工作了一段时间,我只是可以让它工作。 如果有人能帮我写这段代码,我将不胜感激 输入文件记录格式:C++ 无限循环误差。输出文件中没有任何内容,c++,C++,我的代码在第二个while循环中不断中断。有人能帮忙吗。 文件输入是学生的姓名,然后是ID,然后是所述学生的成绩。我已经为此工作了一段时间,我只是可以让它工作。 如果有人能帮我写这段代码,我将不胜感激 输入文件记录格式: Bob Marley 1234567 5 75 90 100 源代码: #include <iostream> #include <fstream> #include <string> using namespace std; in
Bob Marley
1234567
5 75 90 100
源代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
ofstream outputFile;
string studRecord;
string stud_ID;
int count_sat = 0;
int count_Average = 0;
int count_unSat = 0;
int stud_count = 0;
int validGrade = 0;
int invalidGrade = 0;
int grandTotal = 0;
double grade_average = 0;
double class_average = 0;
inputFile.open("StudRecord.txt");
outputFile.open("GradeReport.txt");
if(inputFile.is_open())
{
while (!inputFile.eof()) //getline (inputFile,studRecord)
{
getline (inputFile,studRecord); //Getting name
outputFile << studRecord << '\n';
stud_coups nt++;
getline (inputFile,stud_ID); //Getting ID
int grade;
while (inputFile.peek() != '\n')
{
//inputFile.get();
inputFile >> grade;
if(grade < 0 || grade > 100)
{
invalidGrade++;
}
else
{
validGrade++;
}
}
//inputFile.get();
if(validGrade != 0 )
{
grade_average = stud_count / validGrade;
}
grandTotal += grade_average; // grandTotal = grandTotal + grade_average
if (grade_average >= 85 || grade_average <= 100)
{
count_sat++;
//cout <<" Progress : Satisfactory" << endl;
}
else if (grade_average >= 65 || grade_average <= 85)
{
count_Average++;
//cout << " Progress : Average" << endl;
}
else
{
count_unSat++;
//cout << "Progress : Unsatisfactory" << endl;
}
validGrade = 0;
invalidGrade = 0;
grade_average = 0;
stud_count= 0;
}
}
else
cout << "Unable to open file";
class_average = grandTotal / stud_count;
outputFile << studRecord << endl;
outputFile << "Number of Valid Grades: " << validGrade << endl;
outputFile << "Number of Invalid Grades: " << invalidGrade << endl;
outputFile << "Grade Average: " << grade_average << endl;
outputFile << "Progress: 85-100: Satisfactory, 65-85: Average, 0-65: Unsatisfactory " << endl;
outputFile << "________________________________\n" << endl;
outputFile << "Student Count: " << stud_count << endl;
outputFile << "Satisfactory Count:" << count_sat << endl;
outputFile << "Average Count: " << count_Average << endl;
outputFile << "Unsatisfactory Count:" << count_unSat << endl;
outputFile << "Class Average:" << class_average << endl;
inputFile.close();
outputFile.close();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入文件;
流输出文件;
字符串记录;
串螺柱;
int count_sat=0;
int count_Average=0;
int count_unSat=0;
int螺柱计数=0;
int validGrade=0;
int invalidGrade=0;
int grandTotal=0;
双级平均值=0;
双类平均值=0;
inputFile.open(“StudRecord.txt”);
outputFile.open(“GradeReport.txt”);
if(inputFile.is_open())
{
而(!inputFile.eof())//getline(inputFile,studRecord)
{
getline(inputFile,studRecord);//获取名称
输出文件等级;
如果(等级<0 | |等级>100)
{
残疾等级++;
}
其他的
{
validGrade++;
}
}
//get();
如果(有效等级!=0)
{
平均等级=螺柱计数/有效等级;
}
grandTotal+=等级\平均;//grandTotal=grandTotal+等级\平均
如果文件流结束时(grade|u average>=85 | | grade|u average,peek()
将返回-1,这将导致无限循环,您应该像这样测试它:
int next_char;
while (next_char = inputFile.peek(), (next_char != '\n') && (next_char != -1) )
我不知道为什么要重置这些变量:
validGrade = 0;
invalidGrade = 0;
grade_average = 0;
stud_count= 0;
它将导致一个异常:当第二个循环时,整数除以零,而(inputFile.peek()!='\n')
可能到达文件末尾,并由于peek()
返回EOF
而不是'/n'
如果inputFile.peek()==EOF
我希望这会有所帮助!如果您的文件中的数据顺序相同,我建议这样做
while (inputFile.peek() != EOF || (!isalpha(inputFile.peek()))
这将适用于这样的文件
鲍勃·马利
1234567
5 75 90 100
巴拉吉公羊
1265466
4 54 54 98尝试根据eof标志检查peek(),而不是“\n”。std::char\u traits::eof()
输入文件中的每两行之间是否有换行符?