Thread: One problem at a time.

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

    One problem at a time.

    Okay I know this snippit here has a ton of problems but there is one inparticular that is troubling me.

    Under the GATHER DATA section, When a number is entered it is checked via a validate function. Obviously if its not valid then it generates an error. The problem Im having is once Valid function fires and the error is generated, how can I get the NEW number entered to go back through the same while statement to validate this new number?

    Code:
    int main (void)
    {
    	int maxnum;
    	int a;
    	char buff[BUFSIZ];
    
    	printf("How many numbers would you like to sort?\n");
    	scanf("%d", &maxnum);
    	printf("\nPlease begin entering your %d numbers.\n", maxnum);
      
    // GATHER DATA
    	for( a = 0; a < maxnum; a++)
    	{
    		printf( "Enter number %d : ", a + 1 );
    		scanf( buff, BUFSIZ, stdin );
    
    		while( !valid( buff, strlen(buff) ) ) 
    		{
    			printf( "Invalid Integer.\n" );
    			printf( "Please re-enter number %d: ", a + 1 );
    			fgets( buff, BUFSIZ, stdin );
    		}
    
    		sscanf( buff, "%lf", buff );
    	}
    
      return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I'm not sure I understand your question. If the user enters an invalid number, the program prints an error and prompts the user to retype the number. This process is continued for the same item until a valid number is entered, then the next item is recieved:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define BOOL_TRUE  1
    #define BOOL_FALSE 0
    
    static int valid ( char *buf, size_t bsize )
    {
      size_t i;
      for ( i = 0; i < bsize - 1; i++ )
        if ( !isdigit ( buf[i] ) )
          return BOOL_FALSE;
      return BOOL_TRUE;
    }
    
    int main (void)
    {
      int maxnum;
      int a;
      char buff[BUFSIZ];
      
      printf("How many numbers would you like to sort?\n");
      scanf("%d%*c", &maxnum);
      printf("\nPlease begin entering your %d numbers.\n", maxnum);
      
      /* GATHER DATA */
      for( a = 0; a < maxnum; a++)
      {
        printf( "Enter number %d : ", a + 1 );
        fgets( buff, BUFSIZ, stdin );
        
        while( valid( buff, strlen(buff) ) == BOOL_FALSE ) 
        {
          printf( "Invalid Integer.\n" );
          printf( "Please re-enter number %d: ", a + 1 );
          fgets( buff, BUFSIZ, stdin );
        }
    #if 0
        /* This won't work, buff is a char *, not a double */
        sscanf( buff, "%lf", buff );
    #endif
      } 
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

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

    WHOA!

    WHOA! very cool.

    I was using a double at the end because my function (see below) worked better if I used it.
    Code:
    static int valid( char array[], int count )
    {   
    	int i = 0, decCount = 0, negCount = 0;
    
    	for( i = 0; i < count; i++)
      {
    		if( array[i] == '\n' ) continue;
    
    		if(( array[i] < '0' || array[i] > '9' ) && array[i] != '.' && array[i] != '-')
    		return BOOL_TRUE;
    		
    		if( array[i] == '.' )
    			decCount++;
    		else if( array[i] == '-' )
    			negCount++;
    
    		if( decCount > 1 || negCount > 1 )
    			return BOOL_FALSE;
    	}
    	return BOOL_TRUE;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 03:45 PM
  2. another problem with catstr() this time
    By the_winky_files in forum C Programming
    Replies: 19
    Last Post: 09-22-2005, 04:20 PM
  3. time problem
    By sand_man in forum C Programming
    Replies: 9
    Last Post: 09-13-2005, 05:59 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Problem with time on the board, or is it just me?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-23-2002, 05:45 AM