input check

This is a discussion on input check within the C Programming forums, part of the General Programming Boards category; hello how can i make sure that the user inputs the right amount of numbers. what i mean is: the ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    input check

    hello
    how can i make sure that the user inputs the right amount of numbers.
    what i mean is:
    the input needs to be 2 int numbres with space between them. for example:
    4000 5000.
    and i want to make sure that if the user input only one number and press return,

    it will print "ERROR" and the program return 1.

    thank you for your help

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    There are many ways. One way is reading the entire line of input to a string and then checking the content of that string to determine if it can be interpreted as two ints with an intervening space.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( scanf( "%d %d", &n1, &n2 ) == 2 )
        ...yay...
    else
        ...error...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  3. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21