我无法准确指出我的文件输入出错了。下面是代码:ifstream不规则模式
char tempFirst[20], tempLast[20], tempCourse[7];
char c; // For peeking
// Find out how many students are in the file
inFile >> numStudents;
for (int i = 0; i < numStudents; i++)
{
// Get last name from file
inFile.getline(tempLast, 20, ',');
// Gets rid of any spaces inbetween last and first
while (isspace(inFile.peek()))
c = inFile.get();
// Get first name from file
inFile.getline(tempFirst, 20, '\n');
// New line, get course
inFile >> tempCourse;
// PRINT
cout << tempFirst << "\n" << tempLast << "\n"
<< tempCourse << "\n";
list[i]->SetGrades(inFile);
}
SetGrade导致了这三种继承功能之一:
void EnglishStudent::SetGrades(ifstream& inFile)
{
inFile >> attendance >> project >> midterm >> final;
cout << attendance << " " << project << " " << midterm << " " << final << "\n\n";
}
void HistoryStudent::SetGrades(ifstream& inFile)
{
inFile >> paper >> midterm >> final;
cout << paper << " " << midterm << " " << final << "\n\n";
}
void MathStudent::SetGrades(ifstream& inFile)
{
inFile >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5
>> test1 >> test2 >> final;
cout << quiz1 << " "<< quiz2 << " " << quiz3 << " " << quiz4 << " " << quiz5
<< " " << test1 << " " << test2 << " " << final << "\n\n";
}
这里是我从加载的信息的文件:
6
Bunny, Bugs
Math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
History 88 75 90
Dipwart, Marvin
English 95 76 72 88
Crack Corn, Jimmy
Math 44 58 23 76 50 59 77 68
Kirk, James T.
English 40 100 68 88
Lewinsky, Monica
History 60 72 78
然后在这里是输出:
Bugs
Bunny
Math
90 86 80 95 100 99 96 93
Joe
History
88 75 90
Marvin
English
95 76 72 88
Jimmy
Crack Corn
Math
44 58 23 76 50 59 77 68
James T.
English
40 100 68 88
Monica
History
60 72 78
我错过了大多数姓氏,对于第一个学生,名字包含了一个结尾。我将如何去解决这个问题?
如何创建'list'并设置'名单[I]'?特别是,当我用'list'的适当定义和操作填写了[示例代码](http://sscce.org/)时,它打印了最后的名字。 – outis
将'SetGrades'方法的参数设置为'istream'会更好,所以它们会更通用。这将允许他们使用'cin'和'istringstream's。 – outis
是否允许使用'std :: string'? (因为我喜欢[这个'getline()'](http://www.cplusplus.com/reference/string/getline/)多于'istream :: getline()') – moooeeeep