Thread: Testing user input question?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    Testing user input question?

    I have a program that requires a user to input a number. The number determines how many random numbers(done by my program) will be displayed on the screen. The number input by the user will be of type int. Is it legal to use an isalpha() function to test and see if it actually is an int. Eg. if someone types in 46i instead of 461. Would this be a correct way to check it. I'm not at my PC with my compiler right now so I'm just trying to get close.

    Code:

    Code:
    void get_info(char *filename, int *n_ptr)
    {
        prinf("\n%S\n\n%s",
    	"This progam creates a file of random numbers.",
    	"Please enter in the number of random numbers
                      you would like to see as a positive integer.   "),
        if (scanf("%d", n_ptr)) != 1 || n_ptr <=0 || isalpha(n_ptr)
            {
                printf("\nERROR: Please enter positive integer.\n");
             }
        
         printf("\nIn what file would you like them?  ");
         scanf("%s", filename);
    }
    Thanks for any help.
    Last edited by Hoser83; 02-15-2006 at 08:22 AM.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    No, that will not work as you expect it to.
    If the user types 46i , the scanf will succesfully read 46 into n_ptr, and leave i and the newline character in the stdin buffer.
    The isalpha test is useless as your != 1 test for scanf will always ensure n_ptr is an integer.
    You'll have to read into a string and then test the input if you want to validate everything up to when the user presses <enter>

    Also, make sure you dereference your pointers when using the value the pointer points to
    Code:
    scanf("%d", n_ptr)) != 1 || *n_ptr <=0 || isalpha(*n_ptr)
    Last edited by spydoor; 02-15-2006 at 09:16 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    fgets() to read a line of input
    strtol() to convert and validate the input.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Given this:

    Code:
    #define   MAXLINE   100
    and

    Code:
    int main(void)
    {
          int   line[MAXLINE];
       .....................
    Would this be correct?

    I'm not really familiar with strtol(). I looked it up, and gave this a try.



    Code:
    void get_info(char *filename, int *line)
    {
        prinf("\n%S\n\n%s",
    	"This progam creates a file of random numbers.",
    	"Please enter in the number of random numbers
                      you would like to see as a positive integer.   "),
        fget( line, MAXLINE, stdin);
        if( strtol( line ) != 1)
            {
                printf("\nERROR: Please enter positive integer.\n");
             }
        
         printf("\nIn what file would you like them?  ");
         scanf("%s", filename);
    }
    Last edited by Hoser83; 02-15-2006 at 11:42 AM.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Hoser83
    Code:
        if( strtol( line ) != 1)
            {
                printf("\nERROR: Please enter positive integer.\n");
             }
    Is 1 the only positive integer in the world?

    ...and yes, you can define an array size with a constant integer.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    I'm not sure I totally understand. I thought that-

    Code:
        if( strtol( line ) != 1)
            {
                printf("\nERROR: Please enter positive integer.\n");
             }

    meant that if strtol returned a 1 or true, that the it would then make the if statment true.
    Last edited by Hoser83; 02-15-2006 at 12:06 PM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    strtol() returns the string converted to a long. Right now, the only number you're accepting is 1 because of that if statement. Whether that's what you want or not, I don't know, but your error message states "Please enter a positive integer". What would an end-user think if this happened:

    Code:
    Please enter in the number of random numbers you would like to see as a positive integer.    2
    
    ERROR: Please enter positive integer.
    ...and for your edit, strtol() returns a 0 if no conversion was made. That doesn't mean it returns a 1 when it makes a conversion. It doesn't. It returns the value of the converted string.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    If you think that this is the wrong application for strtol(), I won't disagree because I don't know for sure. I just want to make a check to see if the user inputs the right thing. for example: 461 is good where 46i is bad. I just want to check for this.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Did your manual page say strtol() only had one parameter?
    http://www.mkssoftware.com/docs/man3/strtol.3.asp

  10. #10
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    no, it doesn't. It says that it take an nptr, endptr, and a base. I just need to figure out how to incorporate the pointers. Since it converts it to a long int, does this mean that if it encounters a character, it just returns a 0?
    Last edited by Hoser83; 02-15-2006 at 12:28 PM.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    If it encounters any character before a numbers it returns 0. (failure)
    If it encounters a number first it acts like scanf and returns upto what it could convert.
    If you additionally pass pEnd you can check to see if extra characters were typed that could not be convereted.
    Since you used fgets pEnd should equal '\n' if no extra characters were typed.

    Code:
    char *pEnd;
    strtol(...,&pEnd,..);
    if(*pEnd != '\n')
      bad

  12. #12
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Okay, let me try that:

    The way I have this now. It takes the string line, and the pointer pEnd, and also the base address.
    If it comes to something other that a character before it reaches pEnd, it returns a 0, which in turn then you get the error message? Is this right?







    Code:
    void get_info(char *filename, int *line)
    {
        char *pEnd;
    
        prinf("\n%S\n\n%s",
    	"This progam creates a file of random numbers.",
    	"Please enter in the number of random numbers
                      you would like to see as a positive integer.   "),
        fget( line, MAXLINE, stdin);
        strtol( line, &pEnd, line[0] ) 
            if(*pEnd != '\n')
             {
                printf("\nERROR: Please enter positive integer.\n");
             }
        
         printf("\nIn what file would you like them?  ");
         scanf("%s", filename);
    }
    Last edited by Hoser83; 02-15-2006 at 12:53 PM.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You're close.
    I guess you are not actually testing this as you go.

    The 3rd paramater should be the number base.
    The Third argument (base) can have a value of 0 or 2-32.

    0 - strtol will attempt to pick the base. Only Dec, Oct Hex supported.
    2-31 - The base to use.
    You've also removed the check for positive number, and after your call to strtol you don't store the value to use anywhere.

    consider this

    Code:
        char *pEnd; long l; char line[BUFSIZ];
    
        printf("Enter a positive int: ");
        fgets(line, BUFSIZ, stdin);
    
        if((l = strtol(line, &pEnd, 0)) <= 0 || *pEnd != '\n') {
            printf("Bad\n");
        }else{
            printf("Your number: %ld\n", l);
        }

  14. #14
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    This may be a dumb question, but what does the BUFSIZ mean. It that a default thing?

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why do you have a character being passed as your radix arguement? That's supposed to be what the base of the string it's trying to convert is. In your case it would be base 10.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  2. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  3. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 08:57 PM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  5. Beginner question- user input termination
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 02:48 PM