Thread: checking input

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    checking input

    what happens if the user is asked to input a number or even comes from a file
    and u wanna check if the input is really numbers and not something liek 124a
    how would u check that
    isdigit wont work on it cuz it requires a character
    cheers

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    >>isdigit wont work on it cuz it requires a character

    try using a loop!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    71
    my input is an int
    not a char array

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you have 2 strategies.
    1) use member functions of ios to decide whether input is good.
    2) get all input as a string and parse what u want from it.

    look up ios::fail(),ios::bad(),ios::clear(),ios::good(), etc. in your helpfiles. For strategy 2 look up stringstreams.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    71
    how would i use the ios::fail and ios::bad here
    i mean how would they be set if the int contains a character?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM
  3. Checking input
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-10-2002, 09:00 AM
  4. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 09:07 PM