I'm new to C++ and I can't seem to find the problem with my code. I have a function that opens an input file. It asks you to input a file and if it can’t open the file it asks you if you want to try again. If you answer y for yes it asks you to enter another input name. My problem is if I don’t enter the correct file name the first time and try again, the file wont open even if the name is correct the second time around. What am I doing wrong? The code is below

{
	string FileName; 
	char tryAgain = 'y';

	while (tolower(tryAgain) == 'y')
	{
		cout << "Please type the name of your input file: ";
		cin >> FileName;	
		cout << endl;
		
		tryAgain = 'n';

		inFile.open(FileName.c_str());
		if(!inFile)	
		{
			cout << "Can't open input file" << endl <<
			        "Would you like to try again? (y or n): ";
			cin >> tryAgain;
			
			while ((tolower(tryAgain) != 'y')&& (tolower(tryAgain) != 'n'))
			{
				cout << endl << "Please enter y or n! ";
				cin >> tryAgain;
			}
		}
	}	
}

I'm new to C++ and I can't seem to find the problem with my code. I have a function that opens an input file. It asks you to input a file and if it can’t open the file it asks you if you want to try again. If you answer y for yes it asks you to enter another input name. My problem is if I don’t enter the correct file name the first time and try again, the file wont open even if the name is correct the second time around. What am I doing wrong? The code is below

[
{
string FileName;
char tryAgain = 'y';

while (tolower(tryAgain) == 'y')
{
cout << "Please type the name of your input file: ";
cin >> FileName;
cout << endl;

tryAgain = 'n';

inFile.open(FileName.c_str());
if(!inFile)
{
cout << "Can't open input file" << endl <<
"Would you like to try again? (y or n): ";
cin >> tryAgain;

while ((tolower(tryAgain) != 'y')&& (tolower(tryAgain) != 'n'))
{
cout << endl << "Please enter y or n! ";
cin >> tryAgain;
}
}
}
}
]

Code tags

[code]

// paste code here

[/code]

You need to clear the errors from the ifstream after the failed file opening before trying to open a file again:
http://www.cplusplus.com/reference/iostream/ios/clear.html

if(!inFile)
{
cout << "Can't open input file" << endl <<
"Would you like to try again? (y or n): ";
cin >> tryAgain;
[COLOR="Red"]inFile.clear ();[/COLOR]

now it opens a file!
do u mean also that after opening a file it must ask the user for another input?

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	string FileName; 
	char tryAgain = 'y';
    ofstream inFile;   //create open type !
	while (tolower(tryAgain) == 'y')
	{
		cout << "Please type the name of your input file: ";
		cin >> FileName;	
cout << endl;
		
		tryAgain = 'n';

		inFile.open(FileName.c_str());
		if(!inFile)	
		{
			cout << "Can't open input file" << endl <<
			        "Would you like to try again? (y or n): ";
			cin >> tryAgain;
			
			while ((tolower(tryAgain) != 'y')&& (tolower(tryAgain) != 'n'))
			{
				cout << endl << "Please enter y or n! ";
				cin >> tryAgain;
			}
		}
	}	
}

now it opens a file!
do u mean also that after opening a file it must ask the user for another input?

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	string FileName; 
	char tryAgain = 'y';
    ofstream inFile;   //create open type !
	while (tolower(tryAgain) == 'y')
	{
		cout << "Please type the name of your input file: ";
		cin >> FileName;	
cout << endl;
		
		tryAgain = 'n';

		inFile.open(FileName.c_str());
		if(!inFile)	
		{
			cout << "Can't open input file" << endl <<
			        "Would you like to try again? (y or n): ";
			cin >> tryAgain;
			
			while ((tolower(tryAgain) != 'y')&& (tolower(tryAgain) != 'n'))
			{
				cout << endl << "Please enter y or n! ";
				cin >> tryAgain;
			}
		}
	}	
}

I don't see this line anywhere in your code:

inFile.clear ();

and this is the line that I think you need if you want to "try again". When you try to open a non-existent file, at least one of the ifstream's error bits are set (I think it's the badbit) and will stay set until you clear it with the command above, so even if you later give the ifstream a valid filename, you still can't open the file unless you have cleared the error bits with the command above. So I am suggesting adding this (see red line) to your code.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	string FileName; 
	char tryAgain = 'y';
    ofstream inFile;   //create open type !
	while (tolower(tryAgain) == 'y')
	{
		cout << "Please type the name of your input file: ";
		cin >> FileName;	
cout << endl;
		
		tryAgain = 'n';

		inFile.open(FileName.c_str());
		if(!inFile)	
		{
                        inFile.clear ();
			cout << "Can't open input file" << endl <<
			        "Would you like to try again? (y or n): ";
			cin >> tryAgain;
			
			while ((tolower(tryAgain) != 'y')&& (tolower(tryAgain) != 'n'))
			{
				cout << endl << "Please enter y or n! ";
				cin >> tryAgain;
			}
		}
	}	
}

See the link I posted in post 2 for a further discussion/example regarding the clear command and an ifstream's error bits.

On another note, is this an ifstream or an ofstream ? You ask for an input file and call the stream inFile , but you define inFile as an ofstream , which is confusing, in this line:

ofstream inFile;

If it is an ofstream , then giving it a filename that doesn't exist isn't a problem. It will create a new file with that filename (assuming it has permission from the Operating System). If this is truly an ofstream , you should rename it and rename your prompt.

If you are dealing with an input file, though, you should declare inFile as an ifstream , not an ofstream .

soroushc, I just noticed that you are not the same person who started the thread. This thread has been marked as "solved" by the person who started it, so if my post above didn't solve your problem, please start a new thread and link this thread to it if you feel it will help. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.