How to error check 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?

EDIT: This program is not complete. I am in the process of going through and completing each separate function. Before moving on to the next function I need to make sure this one is working properly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*Program to determine company's weekly payroll*/

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void OpenTheFile()
{
ifstream inputFile;
string filename;
char letter;
int number;

//Opens the file
inputFile.open("Lab2part2.txt");
cout << "File opened successfully.\n";

//If file opens successfully, process it.
if (inputFile)
{
    //Displays info from file.
    while (inputFile >> letter)
    {
        cout << letter << endl;
    }

    //Close the file.
    inputFile.close();
}
else
{
    //Display error message.
    cout << "Error opening the file.\n";
}
}

void NameTheFile()
{

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

void GetTheData()
{
    //statement
    //statement
    //statement
}

void CalculateTheTotalPay()
{

    //statement
    //statement
    //statement
}

void PrintTheData()
{
    //statement
    //statement
    //statement
}

int main()
{
    //Main to call other functions
    cout << "Welcome.\n";
    NameTheFile();

    return 0;
}
Last edited on
You are looking for a string.

In C++, you can use an std::string for this purpose.

1
2
3
std::string word, line;
std::cin >> word; //using this gets one word from the input
std::getline(std::cin, line); //using this function gets a whole line from the input 


Just note that using operator >> can leave extra junk in the input buffer, so be careful about using it and getline() together.
Topic archived. No new replies allowed.