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

  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
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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
    4,632
    Why isn't it working?


    Jim

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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
    4,632
    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, 05:23 PM