GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Feb-2005, 00:23
jrl134 jrl134 is offline
New Member
 
Join Date: Feb 2005
Posts: 1
jrl134 is on a distinguished road

Help with integer inputs


This may have an easy solution, but I cant figure out how to do this. I am looking for the user to input an integer. The variable is defined as an integer, but when the user enters a character it still works through the code. Is there a way to verify if the user has entered an integer? I would like to possibly display an error message if they enter a character so I would like a way to recognized a character input and for it to react as i wish. Im working with c++ and the cin command for the input. Thanks for your help
  #2  
Old 20-Feb-2005, 00:36
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
You can use the functions located in ctype.h:

CPP / C++ / C Code:
if(!isdigit(character)) printf("Invalid input");

Or create your own:

CPP / C++ / C Code:
#define IS_DIGIT(c) ((c >= '0') && (c <= '9'))

if(!IS_DIGIT(character)) printf("Invalid input");

If the integer'll be more than 1 character long I think you'll just have to loop through the string and test each character.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
base conversions c++ (please help) greensand C++ Forum 10 06-Mar-2005 08:11
How to create integer variables OETN FLTK Forum 4 26-Dec-2004 10:04
Best way to use integer constants crystalattice C Programming Language 5 29-Aug-2004 17:48
converting binary to integer... tru504187211 C++ Forum 3 10-Feb-2004 07:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:58.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.