Thread: Loop reading input

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Loop reading input

    Hi everyone,

    At first, pls excuse my bad english, hope u can understand what i mean.

    im trying to write a function doing the following:

    - loop asking for user input, which should be a unsigned long (e.g.: 1234543,123131,12,123144)

    - that input MUST only be numbers and no other characters, the length of the input cannot be determined BEFORE the input

    - if the input is empty or consists of anything else but only numbers, i have to write an error msg
    - after that error msg, i have to ask again for the input

    i am trying to do this for hours, but i constantly get errors.
    At first, i tryed it with a scanf in the loop, but apparently scanf cant ask for user input in a loop

    Has anyone some tips or ideas how I can do it?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Please post your attempt.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Code:
    #include <stdio.h>
    
    int main()
    {
      unsigned long user_input = 0;
      unsigned long tmp = 0;
    
      printf("\nid? ");
      tmp = scanf("%ld", &user_input);
      while (tmp != 1)
      {   
          printf("wrong id, please enter a new one");
          printf("\nid? ");
          tmp = scanf("%ld", &user_input);    
      }
    
    
    
    
      //printf("\n%ld\n", user_input);
      //printf("%ld", tmp);
       return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    scanf format specifier for unsigned long is %lu.
    Btw, don't declare tmp as unsigned. since scanf() may return EOF(negative value) .

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    applied your changes (ld -> lu, tmp is now long)
    still infinite loop when i enter anything BUT numbers

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As you've probably noticed, input like "1f" will be accepted just fine by what you have. That's both the good thing and the bad thing about scanf: it will try to match what it can to the input, and as long as something matches, then you'll get a result.

    If you want to consider the whole thing they type in, then you'll need to make sure you get all the input. You can either read in the whole line, and then verify the input; or you can read in a character at a time and process as you go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in a string from an input file?
    By matthayzon89 in forum C Programming
    Replies: 5
    Last Post: 08-31-2010, 02:14 PM
  2. Input continuation loop
    By Sinosizer in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 11:54 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. reading an input line using scanf() with %c
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 04-04-2004, 03:10 PM