Reputation: 233
I am having trouble pinpointing where exactly my file input is going wrong. Here is the code:
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 leads to one of these three inherited functions:
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";
}
Here is the file I am loading the information in from:
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
Then here is the output:
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
I am missing most last names and for the first student, firstname has an endline included. How would I go about fixing this?
Upvotes: 0
Views: 362
Reputation: 77420
It's not that the first name has a newline at the end, it's that the last name has a newline at the beginning. When the int
s are read from the input, any whitespace encountered that marks the end of the int
is left in the input stream.
To fix this, either remove whitespace before reading the last name, in the SetGrades
methods or at the end of the loop. The latter two will also require removing whitespace after reading numStudents
. The easiest way of removing whitespace is with the ws
stream manipulator. All it takes is:
inFile >> ws;
You can also replace your peek
ing loop with this.
Replace the character arrays with strings for a more genuine C++ experience. This also necessitates replacing ifstream::getline
with the getline
free function. As a bonus, your code will work for names longer than 19 characters.
std::string tempFirst, tempLast, tempCourse;
...
for (int i=0; i < numStudents; ++i) {
inFile >> std::ws;
getline(inFile, last, ',');
inFile >> std::ws;
getline(inFile, first, '\n');
...
Upvotes: 1
Reputation: 1989
skip the rest of current line before you move to the next line
inFile >> numStudents;
std::string line;
std::getline(inFile, line);//read the rest of the first line, you should do this before you start to read next line
for (int i = 0; i < numStudents; i++)
{
std::getline(inFile, line); //line contains first name and last name
size_t pos = line.find(",");
std::cout << line.substr(0, pos) << std::endl //first name
<< line.substr(pos + 2) << std::endl; //last name,skip ", "
std::getline(inFile, line); // course name and grades
//you could split the course name and grades now with line
std::cout << line << std::endl;
}
Upvotes: 1