// This program shows how to reset fin if it fails so it can be utilized again. // (same idea will also work with cin) // It also shows how to put a limit (in this case 3 times) to the number of times you // check for correct input #include #include using namespace std; int main() { int i, tries = 0; ifstream fin; char filename[30]; cout << " enter name of input file" << endl; cin >> filename; fin.open(filename); tries++; // first try while ((!fin)&& (tries < 3)) { cout << " couldn't open, please enter file name again " << endl; cin >> filename; fin.clear(); // reset status flags for fin - i.e. make it active again fin.open(filename); tries++; // next try } if (!fin) // they had 3 tries - still couldn't get it - time to stop { cout << " After 3 tries let's give up - check your input file" << endl; return 0; } cout << " file opened successfully " << endl; fin.close(); return 0; }