here is the deal, if you press enter, no prob, it waits for a filename, if you enter a bad filename, fin fails and it loops to ask for a valid filename, however, if you then enter a valid filename, it continues to loop regardless.
Here is the code:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout << "enter filename: ";
string filename;
getline(cin, filename);
while (filename.empty()) getline(cin, filename);
ifstream fin(filename.c_str());
while (fin.fail())
{
cout << "ERROR: enter filename: ";
getline(cin, filename);
while (filename.empty()) getline(cin, filename);
ifstream fin(filename.c_str());
}
return 0;
}
I think the problem is that fin does not get rediefined, but i am not sure how to get around it. Thanks
-quizkiwi