Thread: Need help please. Need to validate only numerical input.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    9

    Need help please. Need to validate only numerical input.

    hello fellow coders. i have a slight problem with getting to validate the input.
    the thing is, i want the user to be able to input only numbers in here:

    Code:
    printf("ENTER GUESS 1: ");
    scanf("%d",&guess[0]);
    
    printf("ENTER GUESS 2: ");
    scanf("%d",&guess[1]);
    
    printf("ENTER GUESS 3: ");
    scanf("%d",&guess[2]);
    
    printf("ENTER GUESS 4: ");
    scanf("%d",&guess[3]);
    could someone with more experience please help.
    much appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    thnx for that. ive read through it, but the thing is i dont really understand how i could do it. the thing is i actually want the user to be able to input only numbers, and if they input a letter an error message to display and then for them ot be able to try again.
    perhaps someone has done this before? and they could help?
    thnx.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So read in the input as string, then check if the string consists entirely of numbers. If so, convert it to a number type, else get the user to try again.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the thing is i actually want the user to be able to input only numbers
    You can't force the user to input only what you expect in standard C. You can certainly use a non-standard non-blocking read to enforce that kind of behavior:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <conio.h>
    
    #define ESC 0x1B
    
    int main ( void )
    {
      int ch;
    
      printf ( "Enter a number: " );
      fflush ( stdout );
    
      while ( ( ch = getch() ) != ESC ) {
        if ( isdigit ( ch ) )
          putchar ( ch );
      }
    
      printf ( "\n" );
    
      return 0;
    }
    But that's not portable, and I'd question why reading a string completely and then validating it using standard functions isn't sufficient.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    yes, ummm. i think i know what i have to do know.
    read the string completely, check if its only numbers, if not display error and try again.

    could someone perhaps point me in the right direction how i could go about doing this.
    thnx.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can start by looking here: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Todd

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    thnx guys.
    i used this:
    Code:
    static void die ( char *msg )
    {
      fprintf ( stderr, "&#37;s\n", msg );
      exit ( EXIT_FAILURE );
    }
    
    int main ( void )
    {
    int guess[4];
    
    printf("ENTER GUESS 1: ");
    		if (scanf("%d",&guess[0]) != 1)
    			die ("INVALID");
    }
    the above code works fine, but it could be improved.
    at the moment it displays INVALID and then quits.

    can someone please tell me how i can prevent it from quitting, but rather ask the user to input GUESS 1 again??
    thnx.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Did you try a loop?
    Code:
    int valid = 0;
    
    while ( !valid ) {
      printf("ENTER GUESS 1: ");
      fflush ( stdout );
    
      if (scanf("%d",&guess[0]) == 1)
        valid = 1;
      else
        printf ( "INVALID\n" );
    }
    My best code is written with the delete key.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    static void die ( char *msg )
    {
    	fprintf ( stderr, "&#37;s\n", msg );
    	exit ( EXIT_FAILURE );
    }
    
    int main ( void )
    {
    	int guess[4];
    	printf("ENTER GUESS 1: ");
    	if (scanf("%d",&guess[0]) != 1)
    		die ("INVALID");
    }
    You should also learn to indent properly and not mix spaces and tabs--you can use one.
    Oh and, you know, think a little of how the program works. If I entered something in an app and it wasn't valid input and the program quit saying "invalid data", I'd be rather annoyed. Wouldn't you?
    Yes, the program should say invalid data, but it shouldn't quit.
    (And what's with a static function? I don't think I've encountered a static global function before.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And what's with a static function?
    It's a function with internal linkage. Functions have external linkage by default, but sometimes you don't want them to be visible to everyone.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    Quote Originally Posted by Prelude View Post
    Did you try a loop?
    i used that code but then it just kept on looping and saying invalid loop after loop unable to stop.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Prelude View Post
    >And what's with a static function?
    It's a function with internal linkage. Functions have external linkage by default, but sometimes you don't want them to be visible to everyone.
    Oh, right. In C everything is external by default. I completely forgot.
    Thanks for clearing that up, though.

    Quote Originally Posted by oli3 View Post
    i used that code but then it just kept on looping and saying invalid loop after loop unable to stop.
    Did you try reading a line from the input with fgets?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i used that code but then it just kept on looping and saying invalid loop after loop unable to stop.
    My bad, I was in too much of a hurry. When scanf errors, it leaves the unconverted characters in the stream. So you have to remove them or scanf will continue to error on the same bad input:
    Code:
    int valid = 0;
    
    while ( !valid ) {
      printf("ENTER GUESS 1: ");
      fflush ( stdout );
    
      if (scanf("%d",&guess[0]) == 1)
        valid = 1;
      else {
        printf ( "INVALID\n" );
    
        while ( getchar() != '\n' )
          ;
      }
    }
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    Quote Originally Posted by Prelude View Post
    >i used that code but then it just kept on looping and saying invalid loop after loop unable to stop.
    My bad, I was in too much of a hurry. When scanf errors, it leaves the unconverted characters in the stream. So you have to remove them or scanf will continue to error on the same bad input:
    Code:
    int valid = 0;
    
    while ( !valid ) {
      printf("ENTER GUESS 1: ");
      fflush ( stdout );
    
      if (scanf("&#37;d",&guess[0]) == 1)
        valid = 1;
      else {
        printf ( "INVALID\n" );
    
        while ( getchar() != '\n' )
          ;
      }
    }
    thats working 100%..
    u just dont know how thankful i am!!! now im just gonna implement it into the other 3 guesses.
    cheers !
    Last edited by oli3; 01-11-2008 at 03:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM