While loop being skipped

This is a homework assignment for my CS class. The program basically reads lines of data from a file and calculates averages. I am attempting to go a little bit further and write the program so that if the user enters an incorrect path/input file name, the program will prompt them to reenter the path/name or press q to quit, but for some reason when I reenter the correct file name, it skips over the while loop immediately following which is an echo of the file data. Everything else is working great, later on in the program it calculates the averages and completes just fine. Here is a snippet:

//Open input file

inputFile.open(inputFileName.c_str());



//Check to make sure input file is valid

if (! inputFile.is_open())

{

cout << "The inputfile path or name is invalid. " << endl;

cout << "Reenter file location\\name or press q to quit. ";

getline(cin, reply);



if (reply.substr(0, 1) == "q" || reply.substr(0, 1) == "Q")

{

return 0;

}

else

{

inputFileName = reply;

inputFile.open(inputFileName.c_str());

//Rewind file

inputFile.clear(); //clear end of file flag

inputFile.seekg(0); //go back to beginning of file



}

}

//Read and echo data from input file

while (inputFile.peek() != EOF)

{

getline(inputFile, line);

cout << line << endl;

}
Last edited on
got it by inserting:

//Rewind file

inputFile.clear(); //clear end of file flag

inputFile.seekg(0); //go back to beginning of file

as shown.
instead of saying seekg(0), try seekg(ios::beg)
Last edited on
Topic archived. No new replies allowed.