Scanf and integer...

This is a discussion on Scanf and integer... within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to obtain a number using scanf. But, I want to make sure the input is only ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    Scanf and integer...

    Hi,

    I am trying to obtain a number using scanf. But, I want to make sure the input is only integer, not characters. I have made some progress on C, but its loopping indefieniently and have some errors.
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>


    int get_number(void);


    int get_number()
    {
    int check = 0, number = 0;

    do
    {
    if( check > 0 )
    {
    if (number < 0)
    {
    printf("Not permitted!!! \n");
    printf("Number value cannot < 0 \n");
    }

    }

    fflush(stdin);
    printf("\n");
    printf("Please enter a number: ");
    scanf( "%d", &number);

    while ( (scanf ("%d", &number) ) == 0)
    {
    printf("Entered value is not integer");
    printf("Please enter a number: ");
    scanf( "%d", &number);
    fflush (stdin);
    }

    check = 1;

    } while ( ( (scanf ("%d", &number) ) == 0) || (number < 0) );

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    that's perfectly fine,

    However, I prefer not to use scanf as it will ruin the rest of the code...

    I'm trying to work this out by using the above code... can you help me?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by penny
    that's perfectly fine,

    However, I prefer not to use scanf as it will ruin the rest of the code...

    I'm trying to work this out by using the above code... can you help me?
    What? Make up your mind. Above you said you wanted to use scanf. So do you, or don't you? It can't be both.
    fflush(stdin);
    Don't flush input streams. It's wrong.

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

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You cannot use scanf() to test for invalid input, as it will leave that in the input buffer. For example, entering 9z0 will result in scanf() reading in the 9 and stopping. The functions return code will be 1 denoting success.

    To test properly, use fgets() and strtol(), all as per the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() protection help
    By DSman in forum C Programming
    Replies: 2
    Last Post: 10-09-2007, 01:58 PM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 12:40 AM
  4. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM
  5. scanf ?
    By wagner in forum C Programming
    Replies: 4
    Last Post: 02-20-2002, 12:16 PM

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