Thread: Ensuring user input is integer value (newbie question)

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    Ensuring user input is integer value (newbie question)

    Hi,

    I'm working on a tic-tac-toe program and one of my modules needs to take an integer value from the user and confirm it was valid integer input and print an error message if it is wrong and prompt the user to enter again.

    I am not allowed to use a string and my other issue is that the function prototype has already been defined and needs to be used as is. I would use a IsDigit function and read the value entered as a char and check if it is between char 0 and char 9 but the input (parameters) have to be in int form.

    PRE: User has been prompted to enter an integer and min <= max.
    POST: an integer value, i, entered by user is returned, where min <= i <= max.
    int GetIntInput(int min, int max)


    Any ideas would be amazing. I'm just not sure how to do this without a string or reading each value in the input stream as a char.

    Thank you!

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I'd use sscanf myself and exclaim that it's dangerous to read numerical data directly. :P All kidding aside, I guess (though not positive) you could try something like

    [oops]forget rewind(stdin) - should have searched the forum first.[/oops]

    Code:
    int val;
    
    while((val = fgetc(stdin)) != EOF && val != '\n') {
       if(!isdigit(val)) {
          /* not a number */
       } else if(/* check the range of val */) {
           ...
       } else {
          /* do something with val */
    }
    maybe something to that effect.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,730
    You could use this:
    Code:
    if (!scanf("%d", &val)) printf("What you typed is no number!");
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    scanf, sscanf etc are not suited for numeric input validation since they do not detect overflows.

    Read in the number as a string and use strtol/strtoul to convert the input to a long integer, and then compare that result to what your allowed input range is.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Ronin was actually pretty close. It's a fact that you can subtract '0' to get the numerical version of any decimal digit, and isdigit only returns true for decimal digits.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    Thank you! Sorry for the late reply. I figured out my issue and accidently forgot I posted this!
    I really appreciate the insight though! :-)

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    int readLong(long *l)
    {
      char *e,in[64];
      fgets( in,64,stdin );
      if(!strchr(in,'\n')) while( getchar()!='\n' );
      else *strchr(in,'\n')=0;
      errno=0;
      *l=strtol(in,&e,10);
      return *in&&!*e&&!errno;
    }
    checks all what you want, and now its easy with for your function:
    Code:
    int GetIntInput(int min, int max)
    {
      long l;
      if( !readLong(&l) || l<min || l>max ) return INT_MIN; /* signals an error value */
      return l;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. Input Validation Question
    By zackboll in forum C Programming
    Replies: 14
    Last Post: 10-12-2004, 12:05 AM