Problem with function for safe input.

This is a discussion on Problem with function for safe input. within the C Programming forums, part of the General Programming Boards category; I'm trying to validate the input and only accept numeric entries at the set length. I'm getting weird behaviour if ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    Problem with function for safe input.

    I'm trying to validate the input and only accept numeric entries at the set length. I'm getting weird behaviour if characters are inputed though. The while loop seems to execute two rounds per character. Can anyone see what the problem might be.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    #define LENGTH 4
    #define ZERO 48
    #define NINE 57
    
    int main()
    {
            int stringLength = LENGTH + 4;
            char string[stringLength];
            unsigned int n, i = 0;
            bool valid;
    
            do{
                    valid = true;
                    printf("Enter a %d digit number: ", LENGTH);
    
                    for(i = 0; i < LENGTH; i++){
                            string[i] = getchar();
                            if(string[i] < ZERO || string[i] > NINE){
                                    valid = false;
                                    break;
                            }
                    }
            }while(!valid);
            string[i] = 0;
    
            n = strtol(string, NULL, 0);
    
            printf("%d\n", n);
    
            return 0;
    }

    Also, are there some good guidlines to be found somewhere for making generic safe input functions?
    Last edited by Subsonics; 11-01-2009 at 10:43 AM.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yep, you need to flush the input buffer.
    I usually just use

    Code:
    rewind(stdin);
    but here are better ways.
    Last edited by Spidey; 11-01-2009 at 10:55 AM.
    Spidey out!

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Spidey View Post
    Yep, you need to flush the input buffer.
    I usually just use

    Code:
    rewind(stdin);
    but here are better ways.
    Thank you, I added the while statement from the link after my if statement, and it worked perfectly.
    I did not know of rewind(stdin); btw, will look that one up.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    BTW Spidey, why is it better to catch the char with getchar() than using rewind(stdin)?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Regarding rewind on stdin: rewind(stdin); ? Right or Wrong?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks Memloop, that pretty much aswers it. I think it's a bit strange that things like this isn't fixed when the standard is renewed though, atleast fflush(stdin).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 11:19 PM
  5. Problem using "cin" in a thread function?
    By Fossil in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2003, 09:08 PM

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