Thread: Reading zeros in valid date function

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Reading zeros in valid date function

    The program below determines if date if valid. It works for all dates without a zero in the number. What am I missing?

    Note: Don't worry about leap year. I have not got to that yet, but have a

    Code:
    #include<stdio.h>
    
    int valid_date(int day,int month, int year)
    {
          if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
            && (day >= 1 || day <= 31) && (year >= 1900 ))
            return 1;
    
          else if ((month == 4 || month == 6 || month == 9 || month == 11)
                  && (day >= 1 || day <= 30)&& (year >= 1900))
              return 1;
    
              else
              {
               return 0;
               }
    }
    
    int main()
    {
    int day, month, year;
    
    printf("\nEnter the Day: ");
    scanf("%2d",&day);
    
    printf("\nEnter the Month: ");
    scanf("%2d",&month);
    
    printf("\nEnter the Year: ");
    scanf("%4d",&year);
    
    if(valid_date(day, month, year))
    {
    printf("\nThis Date is Valid\n");
    }
    else
    {
    printf("\nInvalid date Error:  This date doesn't exists in the calander. \n\n ");
    }
          system("PAUSE");
          return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Review the condition for testing "day of the month" ie 1 <= day <= 30/31.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Think about what constitutes a valid date... The year can be almost anything... The number for the month can be anything 1 to 12. The number of days in a month is your big problem... That I would solve with an array...

    Now, try to think in smaller blobs... try to simplify your conditional statements...
    For example:
    Code:
    int IsDateValid(Day,Month,Year)
      { // return 0 for invalid years
         if (Year < 1) 
           return 0;
         // return 0 for invalid months
         if ( (Month < 1) || (Month > 12) )
           return 0;
    
         // validating the days in a month with array is your task
    
        return 1; }
    See how it's going to return 0 unless it gets all the way to the bottom?
    You conditionals are magintudes simpler and the code is far easier to follow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. date valid program
    By tiger6425 in forum C Programming
    Replies: 4
    Last Post: 09-02-2010, 10:03 PM
  2. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 08:23 AM
  3. valid call to the function
    By kellymart87 in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2007, 04:13 PM
  4. Valid variable and function names
    By sean in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-29-2002, 12:30 PM