Input validation

Hi. How do I prevent user from entering characters or symbols instead of numbers?
int num;
cout<<"Enter a number."<<endl;
cin>>num;

Also, how do I prevent user from entering a number instead of a character?
char c;
cout<<"Enter a character."<<endl;
cin>>c;

Thank you!!!
In the first case, the failbit of std::cin will be set when reading a non-numeric character from the istream into an int.
1
2
3
4
5
6
int num;
cout << "Enter a number." << endl;
cin >> num;

if(cin.fail())
    cout << "You didn't enter a number!" << endl;


Conversely, consider the function isdigit
http://www.cplusplus.com/reference/cctype/isdigit/?kw=isdigit

1
2
3
4
5
6
7
8
#include <cctype>

char c;
cout << "Enter a character." << endl;
cin >> c;

if(isdigit(c))
    cout << "You entered a digit, not a character!" << endl;
Last edited on
#include<cstring>

I try to plug in into my program but it doesn't work

CASE1:
double number[SIZE];
cout<<"Enter the letter number: \n";
cin.ignore();
cin.getline(rec.number, SIZE);
if(cin.fail())
{
cout<<"Enter a number"<<endl;
cin.ignore();
cin.getline(rec.number, SIZE);
}

I tried the cin.fail() and it still accept the character.

CASE 2:
char category[SIZE];
cout<<"Enter the category: \n";
cin.getline(rec.category, SIZE);
if(isdigit(rec.category[SIZE])
{
cout<<"enter characters"<<endl;
cin.getline(rec.category, SIZE);
}

For this one, it doesn't even compile. I think there is something wrong with this-->>
if(isdigit(rec.category[SIZE])
Case 1:
You're misusing cin.getline.

Case 2:
You're missing a closing parentheses in your if statement.

If you could elaborate on what you're trying to do maybe I could provide you more constructive help.

What do you define rec as?

Also, when posting code, put them inside code tags (hint: the <> button to the right).
rec is a structure.

I don't think I am misusing cin.getline in case 1
I mean, can you show me your definition of rec?

Unless rec.number is of type char *, then you're misusing cin.getline.
struct record
{
char number[SIZE];
char category[SIZE];
int qty;
};

int main()
{
record rec;
........
}

Last edited on
Oh okay. You're using getline properly. When you wrote the example in the first post it made me think that record.number would be of type int or double.

Again, use [ code ] tags [ /code ] when posting code, please.

Lets see. You want to input a number, stored inside a char variable. I believe it would make more sense to store it into an unsigned int (~4 billion as the upper limit), which should be more than enough for unique record numbers in your program.
It's beneficial to store it in an int in the event that you want to compare it with other numbers or do arithmetic with it in the future.
The reason case1 did not work for you is because cin.fail is only set when reading something that should be a char into an int, double, etc. Otherwise it can be interpreted as a char anyway. So if you mace record::number an unsigned int, using the cin.fail() technique will work.

Now, in case 2 you have a char array that you want to make sure are all characters. You have a couple options depending on which characters you consider valid for a record category.
Do you want a character to be a-z, A-Z only,
or do you want a character to be a-Z, A-Z, !-), special characters, etc, as well?

If the former, you can use the function isalpha on all the characters in category.
http://www.cplusplus.com/reference/cctype/isalpha/?kw=isalpha
isalpha will return true only if a character is alphabetic.
Note that this function only works on individual characters, not the entire array, so you must call it against each character in category[].

if the latter, you can use isdigit, and check to see if it is not a digit instead.
The same thing applies here as it did with isalpha, it must be called against every character in category[]
Last edited on
For case 1:
I changed my number to double number;
but using cin.fail() still doesn't work.

cout<<"Enter the number: \n";

cin>>rec.number;
if(cin.fail())
{
cout<<" enter a number"<<endl;
cin>>rec.number;
}
I know for a fact that this code works, because I just tested it.

1
2
3
4
5
6
7
8
double num;
cout << "Enter the number: ";
cin >> num;

if(cin.fail())
{
    cout << "Invalid input." << endl;
}


If you input something like "2cab391a", it will tell you invalid input.
If you enter "3.14159" it will not tell you invalid input.

For the third time, please use [ code ] tags [ /code ] when typing code.
Topic archived. No new replies allowed.