View Full Version : Basic C++ Question
kirupa
May 20th, 2003, 11:29 PM
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:
Jubba
May 20th, 2003, 11:36 PM
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...
kirupa
May 21st, 2003, 07:17 AM
thanks ;)
TOOL
May 21st, 2003, 07:29 AM
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...:-\
brainy
May 21st, 2003, 08:16 AM
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.
John Moses
May 21st, 2003, 10:00 AM
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.
kirupa
May 21st, 2003, 07:30 PM
Thanks John! As Tool mentioned, I did receive an infinite loop. I'll try your method as well :)
Cheers!
Kirupa :ub:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.