How to Error Check if user inputs letters?

How do I error check if the user is inputting letters and not numbers?

For example, if the user inputs "Lab.txt" I need to display an error message.

If they input "Lab2part2.txt" then this is correct and what I want.

I've found a lot of information online on how to error check for numbers or a single letter (EX: 1,2,3, etc. or 'A' 'B' 'C') but nothing for actual WORDS or maybe I should refer to it as a string of characters?

Is there any way to do this? Because my program requires I ask the user to input the name of the file. But the way my code is currently set up is even when the user inputs the wrong file name it still opens the file. I want to prevent this from happening so my thought was to error check user input.

Suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

ifstream inputFile;
double filename;

//Asks user for file name.

cout << "What is the file name?\n.";
cin  >> filename;

    //Error checking
    if (filename == 'Lab2part2.txt')
    {
        cout << "Opening the file...\n";
        OpenTheFile();
    }

    else (filename != 'Lab2part2.txt');
    {
        cout << "Error opening the file.\n";
    }
Well, first things first. A double is a number, so it'll never be equal to "Lab2part2.txt". You need to enter into an std::string.

Second, the problem as I see it is that your definition of "correct" and "incorrect" has nothing to do with the string itself. Why is "Lab2part2.txt" correct but not "Lab.txt"?

Third, if you're going to reject all inputs except one, what's the point of even asking for input?
Because my professor requires that we ask the user for the file name.

If you let the user input any filename he wants your program could try to open the file and if it fails to open it could simply display an error message.
Strings on lines 11 and 17 must be double-quoted.

Strings on lines 11 and 17 must be double-quoted.


Thanks! This fixed it!
Topic archived. No new replies allowed.