Doesn't matter -- you still need to know what it is -- not as a constant, obviously, but as a variable. You counted when you were loading them in, so you should know, right?
Doesn't matter -- you still need to know what it is -- not as a constant, obviously, but as a variable. You counted when you were loading them in, so you should know, right?
Either: You put an extra c++ in the loop, or your read-in function is skipping lines.
I think the initialize function is not initializing correctly because I used cout to print the counter variable and it amounted to 9 when it should be 18.
Code:void initialize (ifstream& inFile, studentType list[], studentType studentList[], int& counter, int listSize) { for (counter = 0; inFile >> studentList[counter].Name >> studentList[counter].ID >> studentList[counter].score; counter++) { inFile >> list[counter].Name >> list[counter].ID >> list[counter].score; } cout << counter; }
What does the red, and the green, code do?Code:void initialize (ifstream& inFile, studentType list[], studentType studentList[], int& counter, int listSize) { for (counter = 0; inFile >> studentList[counter].Name >> studentList[counter].ID >> studentList[counter].score; counter++) { inFile >> list[counter].Name >> list[counter].ID >> list[counter].score; } cout << counter; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The red code sets the boundary for the for loop to read until it reaches the end of the file.
The green code states what is IN the file.
Okay so we deleted the red code and just put inFile as the boundary and it worked
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
For the printIt function I just did this:
Code:void printIt (ifstream& inFile, studentType list[], int& counter) { int c; cout << setw(8) << "Name" << setw(15) << "ID" << setw(20) << "Score\n\n"; for (c = 0; c < counter - 1; c++) { cout << setw(8) << list[c].Name << "\t"; cout << setw(8) << list[c].ID << "\t"; cout << setw(8) << list[c].score << "\t" << endl; } }
Yes, and I suppose instead of being careful when using a knife, you just use band-aid's a lot :-)
The correct solution is to put the infile << ... stuff inside the condition of the loop, and NOT have it in the body of the for-loop, so that immediately when you detect that you can't read any more, you exit the loop.
Alternatively, we could use your original loop construct, and then break inside the loop when you can't read any more:
--Code:for (counter = 0; counter < listSize; counter++) { if (!inFile >> studentList[counter].Name >> studentList[counter].ID >> studentList[counter].score) { break; } }
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Is it wrong to do it my way? Like will it be a bad way down the road after completing the other functions?
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.