Thread: need a second pair of eyes to debug

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    need a second pair of eyes to debug

    This is just a piece of the code but I believe the problem the is in the function where '\n' is read along with the input, therefore everthing is FLASE. (please tell me if Im wrong.) So I tried to allow \n but
    Code:
    if (( !isdigit ( *max++)) && max != '\n')
    Didnt work either. Any suggestions are welcome. Thnx

    Code:
    static int validate_whole ( char *max )
    {
    	while ( *max != '\0' )
    		if ( !isdigit ( *max++)
    			return BOOL_FALSE;
    	return BOOL_TRUE;
    }
    
    
    int main (void)
    {
    	int maxnum;
         char max[BUFSIZ];
    
    	printf("How many numbers would you like to sort [2-20]: ");
    	fgets( max, BUFSIZ, stdin);
    	
    	while( validate_whole (max) == BOOL_FALSE )
    	{
    		printf("Invalid entry, The number must be between 2 and 20\n");
    		printf("\nHow many numbers would you like to sort [2-20]: ");
    		fgets( max, BUFSIZ, stdin);		
    	}
    
    #if 1
    		//maxnum = atoi( max );
    		sscanf(max, "%d", &maxnum );
    #endif
    Last edited by RyeDunn; 08-02-2002 at 07:38 AM.
    Ryan

    Good judgement comes from experience, and experience comes from bad judgement.
    - Fred Brooks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Add the following directly after the scanf() call to remove the trailing newline character:
    Code:
    #include <string.h>
    ....
    fgets(......);
    max[strcspn(max, "\n")] = '\0';  /* Add this line */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    very cool but one more thing

    within that same function
    Code:
    static int validate_whole ( char *max )
    {
    	while ( *max != '\0' )
    		if ( !isdigit ( *max++))
    			return BOOL_FALSE;
    	return BOOL_TRUE;
    }
    If I wanted to narrow the range of numbers they can enter. For example between 5 and 30, everything else returns false.

    Obviously this would be much easier if it was an int but because we are dealing with a char and possibly 2 elements (ie.. 10 11 12 etc), Im not sure which is the best way to approach it. With my limited knowledge I could do it the long way but Im hoping you gurus might know a shortcut.
    Last edited by RyeDunn; 08-02-2002 at 08:37 AM.
    Ryan

    Good judgement comes from experience, and experience comes from bad judgement.
    - Fred Brooks

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are various ways to do what you ask. One is to convert the string to an int using atoi(), then you can use a straight forward numeric test.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    37
    somethign like this?


    Code:
     static int validate_whole ( char *max )
    {
    	int maxnum;
    
    	while ( *max != '\0' )
    		if ( !isdigit ( *max++))
    			return BOOL_FALSE;
    
    	maxnum = atoi( max );
    
    	if( maxnum < '2' || maxnum > '20' )
    		return BOOL_FALSE;
    
    	return BOOL_TRUE;
    }
    Ryan

    Good judgement comes from experience, and experience comes from bad judgement.
    - Fred Brooks

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >if( maxnum < '2' || maxnum > '20' )
    nearly, try
    >if( maxnum < 2 || maxnum > 20 )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    sorry

    sorry to keep asking but this has me baffeled.....

    I changed a few things to simplify it and to debug.

    When I put this:
    Code:
    maxnum = atoi( max );
    if( maxnum > 20 )
         return BOOL_TRUE;
    else 
         return BOOL_FALSE;
    it denies ALL integers

    when I use::
    Code:
    maxnum = atoi( max );
    if( maxnum < 20 )  // **LESS THAN 20**
         return BOOL_TRUE;
    else 
         return BOOL_FALSE;
    it accepts ALL integers...

    Any ideas?
    Ryan

    Good judgement comes from experience, and experience comes from bad judgement.
    - Fred Brooks

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your problem is that you increment the max pointer variable within the loop in the validate function. Once you have incremented it to the end of the string, it no longer points to the start of it. Therefore, you cannot use it as a parameter to atoi() because it doesn't point to the right place.

    Try something like this:
    Code:
    static int validate_whole ( char *max )
    {	
    	char *savemax = max;
    	int maxnum;	
    	while ( *max != '\0' )
    		if ( !isdigit ( *max++))
    			return BOOL_FALSE;	
    	
    	maxnum = atoi( savemax );
    	printf ("maxnum %d\n", maxnum);
    	if( maxnum < 2 || maxnum > 20 )		
    		return BOOL_FALSE;	
    	return BOOL_TRUE;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Need an extra pair of eyes ....
    By Whoputthatthere in forum C Programming
    Replies: 5
    Last Post: 06-02-2006, 12:19 PM
  4. need another pair of eyes please
    By dustyd in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2003, 03:12 PM
  5. need a second pair of eyes
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-27-2002, 12:36 PM