Thread: date valid program

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    date valid program

    I have a question with my date valid program, when I put in the date in the format MMDDYY it is supposed to check the MM (month), the DD (day), and the year (year). When I put in the date (ex. 121201) it doesn't go to either of the printf statements instead when I press enter it just goes the next line down without any printf output. When the program worked before, if I put in 121201 it would come out as 12121 which I am not sure why it does that. Any solutions that would get this program to work?

    Code:
    #include <stdio.h>
    
    int main(void) {
    
     int month;
     int day;
     int year;
    
    
    
    printf("Enter the date in the format MMDDYY: ");
    scanf ("%d%d%d", &month, &day, &year);
    
      if( month <= 12 && month > 00 && day <= 31 && day > 00 && year >= 00 && year <= 99 )
      
        printf("\n%d%d%d the date is valid\n\n", month, day, year);
    
      else
       printf("\n%d%d%d the date is not valid\n\n", month, day, year);
    
    
     return 0;
    }

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Wink

    Quote Originally Posted by tiger6425 View Post
    I have a question with my date valid program, when I put in the date in the format MMDDYY it is supposed to check the MM (month), the DD (day), and the year (year). When I put in the date (ex. 121201) it doesn't go to either of the printf statements instead when I press enter it just goes the next line down without any printf output. When the program worked before, if I put in 121201 it would come out as 12121 which I am not sure why it does that. Any solutions that would get this program to work?

    Code:
    #include <stdio.h>
    
    int main(void) {
    
     int month;
     int day;
     int year;
    
    
    
    printf("Enter the date in the format MMDDYY: ");
    scanf ("%d%d%d", &month, &day, &year);
    
      if( month <= 12 && month > 00 && day <= 31 && day > 00 && year >= 00 && year <= 99 )
      
        printf("\n%d%d%d the date is valid\n\n", month, day, year);
    
      else
       printf("\n%d%d%d the date is not valid\n\n", month, day, year);
    
    
     return 0;
    }
    Are you getting a black screen? If so, it's because the construction of your code; scanf() is waiting for the next set of 2 numbers. One way around this is to enter your dates like this: 09 02 10 and it'll work. Since it seems like you are just starting out, I would never use all the conditions in one line, instead break them up into separate lines and then test the condition.

    The way you have your scanf() presented, it can never work in this format: MMDDYY.
    Last edited by Char*Pntr; 09-02-2010 at 09:53 PM. Reason: clarification

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How does
    Code:
    scanf("%d%d%d",&month, &day, &year);
    know that the month, day, and year are two digits each?

    Try
    Code:
    scanf("%2d%2d%2d",&month, &day, &year);
    Jim

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    19
    Ok, I changed the printf statement

    Code:
    printf("Enter the date in the format MM DD YY: ");
    where in between the three date types in the command prompt I put in a space
    when I entered between the MM DD YY.

    The scanf("%2d%2d%2d",&month, &day, &year); did change the results since I entered
    a date which would be 09 13 01 in scanf. The command prompt reads the 09 13 01 as 913 1. With a black space instead of a 0 in front of 9 same with before 1.
    I don't know how the command prompt is displaying the date as 9131. Is there a piece of code
    that would include the 0's?

    I also need to come up with a piece of code that would show that the year is a leap year in the
    2000 generation that 2000 would show as YY 00 lets say leap % 20 or leap % 100 to prove that the year
    would be a leap year.
    Last edited by tiger6425; 09-02-2010 at 10:01 PM.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    Date valid test image of command prompt

    I image of the results from the last post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 12:55 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM