Stuck in an infinite loop while inputing file name.

The program is designed to take a file name as a string. My issue here is with the loop, what I want to do with this program is check and see if the file name is good, or if it is bad. And if it's bad, allow the user to re-enter a new file name, however, after it takes in one bad file, it stays in the loop infinitely. What am I doing wrong? I've spent most of the day trying to figure this out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cout << "Please enter the name of the file you wish to decrypt.\n" << 
			"The file must contain no spaces and be less than 25 characters " << 
			"in length: " << endl;
		cin >> userFile;

		ifstream inputFile;//Using fstream operator to Input File Stream.
		inputFile.open(userFile);//Assigns the file to the sting entered by the user and opens file

		if(!inputFile.good())
		{
		while(!inputFile.good())
		{
			inputFile.close();
			cout << "------------------------------------------------------------\n";
			cout << "\nError!! File name must be no greater than 25 Characters!!!";//Error message print out
			cout << "\nPlease enter the name of the file you wish to decrypt.\n" << 
				"\nPlease make sure you have the correct directory and\n"
				<< "that you have entered '.txt' after your file name."
				<< endl;

			cin >> userFile;
			ifstream inputFile;//Using fstream operator to Input File Stream.
			inputFile.open(userFile);//Assigns the file to the sting entered by the user and opens file

		}
		}
Try this (not tested):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cout << "Please enter the name of the file you wish to decrypt.\n" << 
			"The file must contain no spaces and be less than 25 characters " << 
			"in length: " << endl;
		cin >> userFile;

		ifstream inputFile;//Using fstream operator to Input File Stream.
		inputFile.open(userFile);//Assigns the file to the sting entered by the user and opens file

		
		while(!inputFile.good())
		{
			inputFile.clear();

			cout << "------------------------------------------------------------\n";
			cout << "\nError!! File name must be no greater than 25 Characters!!!";//Error message print out
			cout << "\nPlease enter the name of the file you wish to decrypt.\n" << 
				"\nPlease make sure you have the correct directory and\n"
				<< "that you have entered '.txt' after your file name."
				<< endl;

			cin >> userFile;

			inputFile.open(userFile);//Assigns the file to the sting entered by the user and opens file

		}
		
Thank you very much!!! Was so close!
Topic archived. No new replies allowed.