GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Feb-2004, 12:49
hihellochao hihellochao is offline
New Member
 
Join Date: Feb 2004
Posts: 2
hihellochao is on a distinguished road
Question

Need Help on checking user input


How do you check to see if the user enter invalid input ?
I am doing a program that only except input as integer ..... if the user input float or alphabet the program will reprompt the user to input integer..... How do you check to see if the user input integer or not ?

Thank You!
  #2  
Old 27-Feb-2004, 10:28
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Well how are you going about recieving the input?

If you use scanf, giving it the correct placeholder will cause it to only take what input it's been given,. e.g for to place an integer in input:
CPP / C++ / C Code:
scanf("%d", &input);
If you want to check to make sure it's positive, you'll have to do an if statement, checking the value of input:
CPP / C++ / C Code:
if(input < 0){ 
//you'll have to prompt the user again 
}

If you're using C++, just defining an int, and using the << operator, it'll recieve the first integer the user inputs.

Any good?
GF
  #3  
Old 27-Feb-2004, 11:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Garth Farley
Well how are you going about recieving the input?

If you use scanf, giving it the correct placeholder will cause it to only take what input it's been given,. e.g for to place an integer in input:
CPP / C++ / C Code:
scanf("%d", &input);
scanf() is not a good solution for error checking. Although I've never used it myself, I've heard the best error checking is done with strtol()
  #4  
Old 27-Feb-2004, 13:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Someone once told me that "real programmers don't use scanf()". (Since
it's not very good at handling real-world input, which can have errors.)

Here's a suggestion for C programs:

Use fgets() to read an input line from the user into a char array, then
write a routine that steps through the array, looking for whatever it is
that you require (nothing but decimal digits, in your case).

If the user has done something illegal, you can ask him/her to try again,
or you can bail out of the program (with a polite message telling the user
what you expect), or ...

Once you verify that the input consists of decimal digits, then
convert to an integer, using whatever program device you want
(write a routine yourself, or use sscanf() or atoi() or anything
else that comes to mind).




Dave
  #5  
Old 27-Feb-2004, 14:10
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Quote:
Originally Posted by davekw7x
Someone once told me that "real programmers don't use scanf()". (Since
it's not very good at handling real-world input, which can have errors.)

Very true, but best to keep things simple for beginners. Scanf is the easiest to get a hang of, corresponds to printf, and does type conversion automatically. Once a beginner progresses, and finds all of scanf's "quirks" (or bugs more like) (s)he'll abandon it soon enough.

GF
  #6  
Old 27-Feb-2004, 14:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Garth Farley
Very true, but best to keep things simple for beginners. Scanf is the easiest to get a hang of, corresponds to printf, and does type conversion automatically. Once a beginner progresses, and finds all of scanf's "quirks" (or bugs more like) (s)he'll abandon it soon enough.

GF
Neverthess, scanf() can't, by itself, detect certain illegal inputs. In this
case he specifically said
Quote:
if the user input float or alphabet the program will reprompt the user to input integer

If this is a class assignment, I don't see how he can satisfy it with
scanf(), since a user input of, say 12.34, stores the integer value 12
in the target variable, (and doesn't complain).
 
 

Recent GIDBlogWriting a book 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
Win2k domain user dont appear in WinNT domain VoooDooo Computer Software Forum - Windows 5 18-Feb-2004 08:55
a C input question.. tmike C Programming Language 2 19-Sep-2003 03:39
a C input question tmike C Programming Language 1 16-Sep-2003 03:31
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 03:54

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

All times are GMT -6. The time now is 03:37.


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