I need to find out, because my program deals with numbers, how to block characters from being inputted.

Such as,

int input;
cout<<"Please type in a number."<<endl;
cin>>input; //input should be a number. A letter? Oh no.

if the input would have been a character, BOOM! Program == Dead.

What would be the line of code that blocks the input of characters.
So, could someone help me with this. I've been having problems with it for a long time now, and I want to eliminate that hurdle from my programs forever.

8 Years
Discussion Span
Last Post by StuXYZ
char input[100];
cout << "type in a number: "
cin >> input;
while (input is not number)
    cout << "I said type in a NUMBER! ";
    cin >> input;

hope that helps. To check if it is a letter, toupper will convert the char it's ancii value. e.g.

if (toupper(input) >= 'A' && toupper(input) <= 'Z')

will check if it is a letter

Edited by Reverend Jim: Fixed formatting


I personally would use a string...

#include <string>
string myString;

cout << "Enter a number\n>>";
getline(cin, myStrnig);

And then you may want to readinto the cctype header, which will give you some useful functions like isdigit().



I think that it is easier to get the input as a string and then parse it using a stringstream. If that is successful you have a number. If not then you don't. The problem with isdigit etc, is say you want to enter 1.43e-7. Failes on e and -. [yes it works for positive integers/ but not double or complex, or anything negative etc]

So have a look at this bit of code. If anyone can see a simpler way please post. But all the edge cases are an issue.

// returns 0 on success -1 on failure
int getNumber(const std::string& A,double& N)
std::istringstream testStrm;
if (testStrm.fail())  return -1;

const char xc=testStrm.get();
// was the stream completely consumed or did we reach a space.
return (!testStrm.fail() || isspace(xc)) ?  -1 : 0;

Obviously that can be converted to a template function easily.

It takes input of a string (use getline etc) and successfully takes
"-3.4e-2" and fails (correctly) on "3.4g". That is important since
if you do xStrm>>N; that sets N to 3.4 regardless of the trailing g.
It succeeds on "3.4 " as well and additionally " 3.4 ". Again both expected behaviour.

You might like to change the code, not to change N if the tests don't succeed, in which case use a temporary.

If you want "3.4 4.5" to fail, then you have to test that all remaining characters are not a space. (while loop).

Additional code notes: use fail() not good() since the complete consumption of the input string sets eof.


C/C++ assumes input is valid. If you don't trust the user to do that and you want to protect the program from doing something you don't want it to, then you have to program in data validation. In this case you can use a loop to control input.

bool needInput = true;
   cout << "enter data" << endl;
   cin >> data;
   if(data > x && data < y)
     needInput = false;

The original question set out the problem. What happens when a non-number is entered. e.g. the user types "a" by mistake.

Unfortunately, preet4fun's code does not address that problem, it traps into an infinite loop if that is the case. All the other posters identified a path to a solution. The difficulty is in expressing that succinctly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.