问题描述
基本上,在我的"Roster.h“头文件中,我有一个来自学生类的”SetID“对象数组(包括函数changeScore、SetID、setTotal、setLetterGrade)。在下面附加的函数中,它只读取第一行数据,然后在时间条件下退出。我已经盯着这个问题看了好几个小时了,可能需要第二双(或第三只)眼睛。任何批评都是值得赞赏的,因为我知道我不是最有效的programmer.It,应该注意到"m_studentnum“是在构造函数中初始化为0的私有数据。提前感谢!
码
void Roster::readStudentRecord(string file)
{
ifstream in;
string studentID;
string line;
int ola, cla, quiz, homework, exam, bonus, total, final = 0;
in.open(file.c_str());
getline(in, line);
while (in >> studentID) {
in >> cla >> ola >> quiz >> homework >> exam >> bonus >> total >> final;
m_students[m_studentNum].Student::setID(studentID);
m_students[m_studentNum].Student::changeScore(Student::CLA, cla);
m_students[m_studentNum].Student::changeScore(Student::OLA, ola);
m_students[m_studentNum].Student::changeScore(Student::QUIZ, quiz);
m_students[m_studentNum].Student::changeScore(Student::HOMEWORK, homework);
m_students[m_studentNum].Student::changeScore(Student::EXAM, exam);
m_students[m_studentNum].Student::changeScore(Student::BONUS, bonus);
total = cla + ola + quiz + homework + exam + bonus;
m_students[m_studentNum].Student::setTotal(total);
if (total >= 90) {
m_students[m_studentNum].Student::setLetterGrade('A');
}
else if (total >= 80 && total < 90) {
m_students[m_studentNum].Student::setLetterGrade('B');
}
else if (total >= 70 && total < 80) {
m_students[m_studentNum].Student::setLetterGrade('C');
}
else if (total >= 60 && total < 70) {
m_students[m_studentNum].Student::setLetterGrade('D');
}
else {
m_students[m_studentNum].Student::setLetterGrade('F');
}
m_studentNum++;
}
}数据文件-note,我正在做一个getline来读取数据列的标题
ID CLA OLA Quiz Homework Exam Bonus Total FinalGrade
c088801 10 15 4 15 56 5
c088802 9 12 2 11 46 2
c088803 8 10 3 12 50 1
c088804 5 5 3 10 53 3
c088805 3 11 1 10 45 0
c088806 8 14 2 11 40 -1
c088807 4 12 2 12 48 -2
c088808 10 10 3 11 36 0
c088809 8 8 3 11 39 0
c088810 6 9 4 9 47 3
c088811 8 7 3 13 41 3
c088812 4 11 3 11 37 1发布于 2017-02-15 13:32:00
“总计”和"FinalGrade“列为空,您可以无条件地尝试阅读它们。
当您尝试这样做时,输入将包含下一行中的"ID“,因为它不是整数,导致为流设置failbit标志,导致循环条件为false,循环结束。
一种可能的解决方案是将while行读入字符串,将该字符串放入std::istringstream对象中,并像现在这样读取非空列。然后尝试从输入字符串流中读取可能为空的列。
另一种解决方案,如果这些列应该是空的,就是根本不读取它们。
https://stackoverflow.com/questions/42250601
复制相似问题