DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   need urgent responce please! (http://www.daniweb.com/forums/thread160313.html)

raja289 Dec 1st, 2008 5:52 pm
need urgent responce please!
 
i am designing a code that will calculate tomorrows date but there is some problem with the code,
when i enter
year = 2008 or anyother
month =12
day = 31
it gives tmorrows date as 1/2/2008
please help and tell me if i can do more improvements thanks
#include<conio.h>
#include<stdio.h>
main(void)
{
 int t;
 int y,d=1,i,m=1;
 printf("Enter the Year: ");
 scanf("%4u",&y);
 if(y<0)
 y=-y;
 printf("Enter the Month: ");
 scanf("%2u",&m);
 if(m<=0 || m>=12)
 m=1;
 i=y%4;
 printf("Enter the Day: ");
 scanf("%2u",&d);
 if (d>=1&&d<28)
 d=d+1;
 else if(d==i&&m==2)
 {d=1;
 m=3;}
 else if (d==30&&m==4||m==6||m==9||m==11)
 {d=1;
 m=m+1;}
 else if (d==31&&m==1||m==2||m==3||m==5||m==7||m==8||m==10)
{
 d=1;
 m=m+1;
 }
 else if(d==31&&m==12)
 {
 d=1;
 m=1;
 y=y+1;
 }
 printf("Tomorrow's Date is\n  %d/%d/%d ",d,m,y);
 getch();
}

mrboolf Dec 1st, 2008 6:25 pm
Re: need urgent responce please!
 
You are kicking off December! :P
Check
if(m<=0 || m>=12)
.
It should be
if(m<=0 || m>12)
.

Also check your leap years handling.

Freaky_Chris Dec 1st, 2008 6:46 pm
Re: need urgent responce please!
 
Before you do the above, migrate to the C forum

Chris

Ancient Dragon Dec 1st, 2008 7:54 pm
Re: need urgent responce please!
 
Can you use the functions in time.h? If you can, then just fill in a struct tm and call mktime() to calculate tomarrow
#include <time.h>
...
int main()
{
    int m = 11; // November
    int y = 2008;
    int d = 2;

  struct tm tm;
  // clear all members of the structure
  memset(&tm,0,sizeof(struct tm));

  tm.tm_year = y-1900;
  tm.tm_mon = m-1;
  tm.tm_day = d;
  // increment tomarrow
  tm.tm_mday++;
  mktime(&tm);

Salem Dec 2nd, 2008 1:53 am
Re: need urgent responce please!
 
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
Not to mention, the unindented code which is simply awful to look at.

devnar Dec 2nd, 2008 2:36 am
Re: need urgent responce please!
 
Quote:

Originally Posted by Salem (Post 748556)
Not to mention, the unindented code which is simply awful to look at.

Second that. Also, flow of the code is confusing which is really awful considering the simplicity of the problem.

main returns an integer. So it's
int main()
not just
main(void)
.

This test condition is wrong:
else if(d==i&&m==2)

You aren't checking for leap years, although at one point you do find
i=y%4;
. Comparing this value with the number of days screws up the whole thing.


All times are GMT -4. The time now is 4:16 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC