I am trying to read a set of values from a text file into an array of structures of arrays. The entries are each separated by a ‘\n’, and each entry consists of 3 values, separated by a ‘;’.
The problem is that after correctly reading the first line of file data the program reads the first value from the second line, then seems to fail to read the remaining values. Can you point out the error in my syntax or logic?
The test data appears below.
CS162;Finish Lab 2;9/26/2009
CS201;Take Quiz 1;9/28/2009
After reading in the test data my program’s output is below.
Your tasks are:
Finish Lab 2 for CS162 is due 9/26/2009
CS201
for is due
The loops that read the file into the array and outputs the array contents are below. My complete code will be at the end of the question.
for ( ; InputFile.peek() != EOF; ListSize++ )
{
InputFile.get(TaskList[ListSize].Course, BUFFERSIZE, ';');
InputFile.ignore(BUFFERSIZE, ';');
InputFile.get(TaskList[ListSize].Assignment, BUFFERSIZE, ';');
InputFile.ignore(BUFFERSIZE, ';');
InputFile.get(TaskList[ListSize].DueDate, BUFFERSIZE, ';');
InputFile.ignore(BUFFERSIZE, '\n');
}
cout << "Your tasks are:" << endl;
for ( int Iteration = 0; Iteration <= ListSize; Iteration++ )
{
cout << TaskList[Iteration].Assignment << " for " << TaskList[Iteration].Course << " is due " << TaskList[Iteration].DueDate << endl;
}
Full disclosure, this is for a computer science class. This is why I am not asking for complete code solutions, just help with logic or syntax errors. If I am doing this in completely the wrong way, please point me to documentation to help me. But this does put limitations on my code. The program must use character arrays, not strings.
Perhaps the last get should be:
instead of
Your last field (due date) does not have a semicolon at the end, only a newline.
Update: I suggest you also look into using
getlineinstead ofget. They have similar functionality, but getline will consume the delimiter also, meaning that you won’t need to use the ignore().