im trying to create a program the asks the user for a file name and then opens it and if he enters the wrong name it asks again! but when i purposely enter the wrong file name the first time and then i put in the right file name the second time it still says wrong name!!!
Le CodE:
do{
cout<<"Enter File Name : ";
cin>>nam;
cin.ignore();
filename.open(nam);
if (!filename)
{
cout << "wrong file name";
continue;
}
}while(!filename);
i have the whole program created and this is the only error!!!
please help asap!! i've got to submit my project and if i dont all my hard work will go to waste!
ifstream f;
string filename;
// Open the file
cout << "Enter file name> ";
while (true)
{
getline( cin, filename );
f.open( filename.c_str() );
if (f) break;
cout << "I could not open that file.\n""Please enter a valid file name> ";
}
// Use the file f here.
...
// All done. Close the file.
f.close();
Notice that there is no way to escape until you type a valid file name. You might want to add a way out:
ifstream f;
string filename;
cout << "Enter file name, or nothing to quit> ";
while (true)
{
getline( cin, filename );
if (filename.empty())
{
cout << "OK, you want to quit.\n";
return 1;
}
f.open( filename.c_str() );
if (f) break;
cout << "I could not open that file.\n""Please enter a valid file name, or nothing to quit> ";
}
...