Like Tree1Likes
  • 1 Post By jimblumberg

C Program that output the very next date after Input a date

This is a discussion on C Program that output the very next date after Input a date within the C Programming forums, part of the General Programming Boards category; It's my second week of learning C programming. So, don't laugh at me . I have figured out the algorithm ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Exclamation C Program that output the very next date after Input a date

    It's my second week of learning C programming. So, don't laugh at me . I have figured out the algorithm of the problem, but after coding it's not working. Would you please tell me where's the problem?


    Code:
    #include<stdio.h>
    int main()
    {
        int d, m, y, nd, nm, ny;
        printf("Enter the year: ");
        scanf("%d", &y);
        printf("\nEnter the month: ");
        scanf("%d", &m);
        printf("\nEnter the date: ");
        scanf("%d", &d);
        printf("\nSo, your desired date is: %d/%d/%d", d, m, y);
        if (m%2!=0 && m<=7)
        {
            if (d==31)
            {
                nd=1;
                nm=m+1;
                ny=y;
            }
        }
        else if (m%2==0 && 8<=m<=12)
        {
            if (d==31)
            {
                nd=1;
                nm=m+1;
                ny=y;
            }
        }
        else if (m%2==0 && 4<=m<=6)
        {
            if (d==30)
            {
                nd=1;
                nm=m=1;
                ny=y;
            }
        }
        else if (m%2!=0 && 9<=m<=11)
        {
            if (d==30)
            {
                nd=1;
                nm=m+1;
                ny=y;
            }
        }
        else if (m==2 && y%4==0)
        {
            if (d==29)
            {
                nd=1;
                nm=m+1;
                ny=y;
            }
        }
        else if (m==2 && y%4!=0)
        {
            if (d==28)
            {
                nd=1;
                nm=m+1;
                ny=y;
            }
        }
        else if (m==12 && d==31)
        {
            nd=1;
            nm=1;
            ny=y+1;
        }
        else
        {
            nd=d+1;
            nm=m;
            ny=y;
        }
        printf("\n\nThe next date of your desired date is:\t%d/%d/%d\n", nm, nm, ny);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    Can you tell us what the problem is?

    Edit1: The below code does NOT do what you think it does!
    Code:
    8<=m<=12
    Likely want

    Code:
    (8<=m && m<=12)
    Tim S.
    Last edited by stahta01; 03-22-2013 at 08:42 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    Can you tell us what the problem is?

    Edit1: The below code does NOT do what you think it does!
    Code:
    8<=m<=12
    Likely want

    Code:
    (8<=m && m<=12)
    Tim S.
    Thank you for your help . The problem is: "C Program that output the very next date after Input a date" According to your comment I changed the program a little bit. But still not working

    Code:
    #include<stdio.h>int main()
    {
        int d, m, y, nd, nm, ny;
        printf("Enter the year: ");
        scanf("%d", &y);
        printf("\nEnter the month: ");
        scanf("%d", &m);
        printf("\nEnter the date: ");
        scanf("%d", &d);
        printf("\nSo, your desired date is: %d/%d/%d", d, m, y);
        if (m%2!=0 && m>=1 && m<=7 && d==31)
        {
            nd=1;
            nm=m+1;
            ny=y;
        }
        else if (m%2==0 && m>=8 && m<=12 && d==31)
        {
            nd=1;
            nm=m+1;
            ny=y;
        }
        else if (m%2==0 && m>=4 && m<=6 && d==30)
        {
            nd=1;
            nm=m=1;
            ny=y;
        }
        else if (m%2!=0 && m>=9 && m<=11 && d==30)
        {
            nd=1;
            nm=m+1;
            ny=y;
        }
        else if (m==2 && y%4==0 && d==29)
        {
            nd=1;
            nm=m+1;
            ny=y;
        }
        else if (m==2 && y%4!=0, d==28)
        {
            nd=1;
            nm=m+1;
            ny=y;
        }
        else if (m==12 && d==31)
        {
            nd=1;
            nm=1;
            ny=y+1;
        }
        else
        {
            nd=d+1;
            nm=m;
            ny=y;
        }
        printf("\n\nThe next date of your desired date is:\t%d/%d/%d\n", nm, nm, ny);
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    Why isn't it working?


    Jim
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    If you are NOT going to tell us the problem with your program; then go away and never post on the Internet again.

    Edit: You have some reason to believe your program is NOT correct; TELL US the reason!!!

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Guys, please try to understand. It's not working as the output is not as it should be, for example if input is 23/1/2013 output is 1/1/2013. But it should be 24/1/2013. Same problem for every input
    Last edited by rumel.ehsan; 03-23-2013 at 11:48 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    Why are you playing with the month in most of your if statements. You should first just add one to the day, if that is less than 29 you should be done, after all, all months have at least 28 days.

    Also in your last print statement you seem to be printing the month twice, don't you want to print the day?
    Code:
      printf("\n\nThe next date of your desired date is:\t%d/%d/%d\n", nm, nm, ny);
    Jim

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Thanks Jims. Now works. You are the man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-07-2012, 07:37 AM
  2. Replies: 10
    Last Post: 03-28-2012, 10:30 AM
  3. Replies: 11
    Last Post: 03-27-2012, 11:37 PM
  4. Two date objects showing same date?
    By dxfoo in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2010, 06:06 PM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM

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