Thread: do while validation problem

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    do while validation problem

    Hey guys having problems with my do while loop. I am trying to do some validation which works but however i am trying to ask the input to repeat itself if the input is invalid. But if it is valid break out of the do while and proceed with the rest of the code. I have no idea what i can put in the while(); part to do this.

    Code:
     printf("\nPlease Enter your age in dd/mm/yyyy format:");
    
      fgets(input, MAXDATE, stdin);
    
      do
      {
       
       if ( fgets ( input, sizeof input, stdin ) != NULL ) 
       {
        printf("is an invalid date!");
       }
    
        if ( sscanf ( input, "%d/%d/%d", &m, &d, &y ) == 3 )
         {
          if ( is_valid ( m, d, y ) )
          {
            printf ( "%d/%d/%d is a valid date\n", m, d, y );
          }
        }
          else
          {
            printf ( "Invalid date\n" );
          }
       }
      while(valid ==1 || input !=1);
    
    
     }
    
    
    int is_valid ( int m, int d, int y )
    {
      static const int  mday[] = {
        0,31,28,31,30,31,30,31,31,30,31,30,31
      };
    
      if ( m <= 0 || m > 12 )
        return 0;
      else if ( d <= 0 || d > mday[m] )
        return 0;
      else if ( y < 1900 || y > 3000 ) /* Arbitrary choices */
        return 0;
    
      return 1;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while(valid ==1 || input !=1);
    1. valid isn't even declared, or assigned in the loop
    2. input is an array, so not comparable to 1

    Try
    Code:
    while ( fgets ( input, sizeof input, stdin ) != NULL ) {
        if ( sscanf ( input, "%d/%d/%d", &m, &d, &y ) == 3 ) {
            if ( is_valid ( m, d, y ) ) {
                printf ( "%d/%d/%d is a valid date\n", m, d, y );
                break;
            } else {
                // range error
            }
        } else {
            // input format error
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    would it be a do while loop still?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sure you can put it into a do while loop, but it's extra effort IMO.
    A do while loop has to run at least once, whereas a normal while loop can exit without doing anything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM