prompt the user

closed account (416bRXSz)
I have to write a code to prompt the user for the name of the input file and continue to ask the user for the name of the input file until a valid input file is entered. If the file name is not entered correctly then an error message is displayed on the screen before the repeat of the prompt. If the file exists, a message has to be displayed to the screen saying the file was opened. I have this in a function and I have to call it 2 times because there are two different files.
Last edited on
1
2
3
4
5
6
7
8
while(!file.is_open())
{
    std::cerr << "Error - " << filename << " is an invalid filename." << std::endl;
    std::cout << "Please enter a valid filename: ";
    std::cin >> filename;
    //possibly clear and ignore in case of completely wrong filename
    //like the user entered 12 a 1
}


*This would be after the inial read or you could even do:

1
2
3
4
5
while(std::cout << "Please enter a valid filename: " && std::cin >> filename
&& !file.is_open())
{
    //error
}
Last edited on
Topic archived. No new replies allowed.