Hi i guys i want to display all the contents of a file excluding one based on user input
say i have 4 records in the file numbered 1 to 4....the user chooses 2 to exclude it outputs records 1,3,4,4 when it should be records 1,3,4 what am i doing wrong here is the code.its basically displaying the last record in the file twice
The extractor does not set the eof bit on the stream until a read fails. When you read addressC for record 4, the read succeeds, so eof is not set. The loop continues, and the next time through, read.eof() remains false. When recordC is read the next time, the file is empty and eof is set. Because nothing is in the file, recordC, fnameC, lnameC and addressC are not modified, and you continue on in your code, when you print out record 4 information a second time. Then when you go back to the top of the loop eof is true, and you exit.
There are a number of ways to fix this. A couple that come immediately to mind:
(1) Check (!read.eof()) before checking (choice != recordC)
(2) Perform "read >> recordC >>..." before the while loop AND right at the end of the while loop. This guarantees that the read is always done before the eof check.
By the way, when you post code, pleas use code tags. Click the "<>" button in the Format selection and past your code between the tags. This will make it easier for us to read and comment on your code.