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;
}
}
}
}