string fileName;
ifstream inFile;
cout << "What file do you want to gather data from? ";
cin >> fileName;
inFile.open( fileName.c_str() );
while( inFile.fail() )
{
cerr << "Could not connect, try again: ";
cin >> fileName;
inFile.open( fileName.c_str() );
}
This should keep bothering the user until the ifstream can open up the file. However, when I type in a file that IS connectable to after messing up the first time, it just keeps saying "could not connect". Example of success and example of failure:
What file do you want to gather data from? myData.txt
... program successful, works fine
---
What file do you want to gather data from? myD.txt
Could not connect, try again: dadasd.txt
Could not connect, try again: myData.txt
Could not connect, try again:
... what the heck, correct input and it still fails?
Yes, my notes has something that says "use .clear when file fails to open"! So I was leaning towards that. However, I do not know where to put inFile.clear(), could you show me which line it belongs in? If I put it anywhere in the while loop, it doesn't help.