我無法準確指出我的文件輸入出錯了。下面是代碼: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