User Entering Numbers

This is a discussion on User Entering Numbers within the C Programming forums, part of the General Programming Boards category; How can I verify that something enetered by a user is a number? I have a program to accept numbers ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    User Entering Numbers

    How can I verify that something enetered by a user is a number? I have a program to accept numbers from the user but I need to check each one as they enter them and make sure they are not enetering letters instead.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ...

    Check if scanf() succeed.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int num;
        
        printf("Num: ");
        if((scanf("%d",&num)) == 1) {
            //all ok.. right input
            printf("You entered a number really, %d\n",num);
        }
        
        else {
            //invalid input.. error handling here
            printf("You entered, invalid data\n");
            printf("Shame on you :)\n");
        }
        
        system("PAUSE");
        return 0;
    }
    or use isdigit() function to check if an input is numbers..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 10:57 PM
  2. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  3. function to prompt user to continue entering data or not
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2002, 07:38 PM
  4. User entering I/O File names at command line
    By Wiggin in forum C++ Programming
    Replies: 6
    Last Post: 04-27-2002, 01:43 AM

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