943,619 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 620
  • C RSS
Dec 1st, 2008
0

need urgent responce please!

Expand Post »
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
  1. #include<conio.h>
  2. #include<stdio.h>
  3. main(void)
  4. {
  5. int t;
  6. int y,d=1,i,m=1;
  7. printf("Enter the Year: ");
  8. scanf("%4u",&y);
  9. if(y<0)
  10. y=-y;
  11. printf("Enter the Month: ");
  12. scanf("%2u",&m);
  13. if(m<=0 || m>=12)
  14. m=1;
  15. i=y%4;
  16. printf("Enter the Day: ");
  17. scanf("%2u",&d);
  18. if (d>=1&&d<28)
  19. d=d+1;
  20. else if(d==i&&m==2)
  21. {d=1;
  22. m=3;}
  23. else if (d==30&&m==4||m==6||m==9||m==11)
  24. {d=1;
  25. m=m+1;}
  26. else if (d==31&&m==1||m==2||m==3||m==5||m==7||m==8||m==10)
  27. {
  28. d=1;
  29. m=m+1;
  30. }
  31. else if(d==31&&m==12)
  32. {
  33. d=1;
  34. m=1;
  35. y=y+1;
  36. }
  37. printf("Tomorrow's Date is\n %d/%d/%d ",d,m,y);
  38. getch();
  39. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
raja289 is offline Offline
54 posts
since Jul 2008
Dec 1st, 2008
0

Re: need urgent responce please!

You are kicking off December!
Check if(m<=0 || m>=12) .
It should be if(m<=0 || m>12) .

Also check your leap years handling.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 1st, 2008
0

Re: need urgent responce please!

Before you do the above, migrate to the C forum

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 1st, 2008
0

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
  1. #include <time.h>
  2. ...
  3. int main()
  4. {
  5. int m = 11; // November
  6. int y = 2008;
  7. int d = 2;
  8.  
  9. struct tm tm;
  10. // clear all members of the structure
  11. memset(&tm,0,sizeof(struct tm));
  12.  
  13. tm.tm_year = y-1900;
  14. tm.tm_mon = m-1;
  15. tm.tm_day = d;
  16. // increment tomarrow
  17. tm.tm_mday++;
  18. mktime(&tm);
Last edited by Ancient Dragon; Dec 1st, 2008 at 7:55 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2008
1

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 2nd, 2008
0

Re: need urgent responce please!

Click to Expand / Collapse  Quote originally posted by Salem ...
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:
  1. 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.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: UNIX Client and Server
Next Thread in C Forum Timeline: Trouble with pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC