# Basic C++ Question

**URL:** <https://forum.kirupa.com/t/basic-c-question/21380>\
**Category:** programming\
**Created:** [May 21, 2003, 3:29am UTC](https://forum.kirupa.com/t/basic-c-question/21380 "2003-05-21T03:29:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [May 21, 2003, 3:29am UTC](https://forum.kirupa.com/t/basic-c-question/21380/1 "2003-05-21T03:29:49Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 21, 2003, 3:36am UTC](https://forum.kirupa.com/t/basic-c-question/21380/2 "2003-05-21T03:36:50Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 21, 2003, 11:17am UTC](https://forum.kirupa.com/t/basic-c-question/21380/3 "2003-05-21T11:17:52Z")

</div>

thanks 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 21, 2003, 11:29am UTC](https://forum.kirupa.com/t/basic-c-question/21380/4 "2003-05-21T11:29:07Z")

</div>

> \*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…:-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 21, 2003, 12:16pm UTC](https://forum.kirupa.com/t/basic-c-question/21380/5 "2003-05-21T12:16:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 21, 2003, 2:00pm UTC](https://forum.kirupa.com/t/basic-c-question/21380/6 "2003-05-21T14:00:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 21, 2003, 11:30pm UTC](https://forum.kirupa.com/t/basic-c-question/21380/7 "2003-05-21T23:30:34Z")

</div>

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

Cheers!  
Kirupa :ub:
