I have a program that prompts the user for an existing text file that they want my program to modify. Currently, when the user puts in a file that can't be found, the program displays an error message then quits. I would prefer to put the prompt in a while loop and while the file can't be found, prompt the user to try to input the file name again. I set up my loop but it doesn't work. THis is how I tried it:
At first I didn't have the fin.close() at the top of the loop but added it when the loop was getting stuck in an infinite loop. I'm thinking that I might have to rename the file variable name each time, i.e.: input_file, input_file2, input_file3...Code:cin>>input_file; fin.open(input_file); while (fin.fail()) { fin.close(); cout<<"Error opening input file. Did you input the wrong name or path?\n"; cout<<"\nEnter the name of the text file: \n\n"; cin>>input_file; fin.open(input_file); }
Is there a better way?



LinkBack URL
About LinkBacks


