I have a code that is used to store information in a .txt file and when the code runs it is suppose to grab the info from that .txt file and display it but every time I run the code it just ends and idk why.
PLEASE USE CODE TAGS (the <> formatting button to the right), when posting code! Along with the proper indenting, it makes it easier to read your code, and thus also easier to respond to your post. Tutorials on how to use code tags: http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/ I've found the second link to be the most help. Hint: You can hit "edit post", highlight your code and then press the <> formatting button. Note: This will not automatically indent your code! That part is up to you! I've found it's easiest to copy and paste pre-indented code directly from the IDE, that way it is already properly formatted. You can use the "preview" button at the bottom to see how it looks. Using Code Tags, @Handy Andy, from cplusplus.com |
|
|
|
|
Record Number: 0 Forename: John Surname: Smith Department: School Tel.No.: 1282828 Record Number: 1 Forename: Owen Surname: Smith Department: School Tel.No.: None Record Number: 2 Forename: Brittani Surname: Smith Department: School Tel.No.: 921421351 Record Number: 3 Forename: Nick Surname: Smith Department: School Tel.No.: I dont know Program ended with exit code: 0 |
while (! myFile.eof()) //loop through data
does not work the way that you are thinking. It will loop 1 extra time before it detects that "eof" has been reached.salem c once wrote: |
---|
eof() is a state (the result of a past event), not a prediction (of a future). If you have a 10 line file, then calling getline 10 times does NOT make eof() true. You have to try and read the file for the 11th time to make eof() become true. That's why you have the i-- "oops, one too many" bodge. |
Record Number: 1 Forename: John Smith School 1282828 Owen Smith School None Brittani Smith School 921421351 Nick Smith School I dont know Surname: Department: Tel.No.: |
|
|
|
|
|
|
Record Number: 1 Forename: John Surname: Smith Department: School Tel.No.: 1282828 Record Number: 2 Forename: Owen Surname: Smith Department: School Tel.No.: None Record Number: 3 Forename: Brittani Surname: Smith Department: School Tel.No.: 921421351 Record Number: 4 Forename: Nick Surname: Smith Department: School Tel.No.: I Don't Know |
These days the (\n) works much the same as "endl" |