Thread: scanf, prevent input letters

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    scanf, prevent input letters

    Hi, how do i prevent them from entering letters when i ask for numbers, using scanf

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    also what does this mean

    printf ("%d", scanf("%d", &var));

    ? Does this return 1 if the variable is filled, and 0 if its null?

    thanks

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by renderstream
    Hi, how do i prevent them from entering letters when i ask for numbers, using scanf
    You can't.
    Read: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    >> printf ("%d", scanf("%d", &var));
    Read up on what scanf() returns to understand what gets printed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Quote Originally Posted by renderstream
    Hi, how do i prevent them from entering letters when i ask for numbers, using scanf
    You can't do it with scanf alone. A common way to pre process input is to read stdin using fgets, scan the buffer for the expected characters, then call sscanf to put the input into the proper variables, or let the user know they messed up and ask them to try again.

    It's a lot of work, and there are easier ways... one reason scanf is seldom used.

    -Rog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. Displaying an input backwards
    By guitarscn in forum C Programming
    Replies: 29
    Last Post: 03-12-2008, 02:18 AM
  3. input from scanf into structure, how?
    By Elite in forum C Programming
    Replies: 8
    Last Post: 03-04-2005, 02:59 PM
  4. Restrict scanf() input?
    By difficult.name in forum C Programming
    Replies: 4
    Last Post: 09-20-2004, 12:27 PM
  5. prevent char input from int
    By SuperNewbie in forum C Programming
    Replies: 1
    Last Post: 04-08-2004, 02:35 AM