I am attempting to write data to a file from a while loop and then read the file back to the output screen. All three loops write to the file BUT it is only the last loop that reads back to the output screen (three times). Here is what I have written, maybe you can tell me where I am going wrong:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
within your last loop. Remove the 2 additional outputs. Also move inFile.close();out of the while loop. Before cout you should add if(!inFile.eof()) otherwise you have one output too much.
There are a couple of errors that stand out above.
1. the file is closed inside the while loop. That means it's guaranteed to fail on the second iteration, regardless of the status of eof() - which has no sensible meaning for a file which is closed.
2. The data is written out in writeFile() with each value separated by a newline.
It should therefore be read back a whole line at a time. Otherwise problems will arise if any of title, artist or genre consists of more than a single word.