do while validation problem

This is a discussion on do while validation problem within the C Programming forums, part of the General Programming Boards category; Hey guys having problems with my do while loop. I am trying to do some validation which works but however ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21