Basic C++ Question

Hey everyone,
Just wondering, does C++ have a way of checking whether data entered is a number? I know Flash has some cool features for interpreting the data entered, but I am wondering if something similar exists in C++.

I am trying to create a portion of a larger program that displays an error when the user enters data that is not an error.

Thanks!
Kirupa :asian:

my friend said to just check if its a valid int with an if statement:

if(num >=0){
//do this…
}

if its not a number it won’t run…

thanks :wink:

*Originally posted by Jubba *
**if its not a number it won’t run… **

im not too sure if this is right…i remember when i was learning c++ last year with basic win32 programs, if the wrong data was entered say a character where a number was supposed to be entered, the program would just go nuts and infinite loop over and over…now maybe that was my programming skills and maybe it is different if you are making it with MFC…:-\

well, unlike actionscript, c++ uses typed variables. so you cant read anything else then a number into an integer. you just cant do it. so a check is really redundant, as that can never happen.

c++ would probably error if data entered won’t be the type it expects it to be, im not sure how it would act in such case.

i registered just to answer this question, thought i should because of some of the good flash stuff on here. anyways…

im assuming you are taking user input and checking if its an integer. if you have this:

int num;
cin>>num;

so if you have the variable labeled as an integer then you would include the <cctype> in the header file and add this statement:

int isIt;
isIt = int isdigit (int num); // 0 for false, 1 for true

if( isIt == 0)
cout<<“error”;

hope this helps. but it has to be initialized as an integer.

Thanks John! As Tool mentioned, I did receive an infinite loop. I’ll try your method as well :slight_smile:

Cheers!
Kirupa :ub: