need urgent responce please!

Reply

Join Date: Jul 2008
Posts: 41
Reputation: raja289 is an unknown quantity at this point 
Solved Threads: 0
raja289 raja289 is offline Offline
Light Poster

need urgent responce please!

 
0
  #1
Dec 1st, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: need urgent responce please!

 
0
  #2
Dec 1st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: need urgent responce please!

 
0
  #3
Dec 1st, 2008
Before you do the above, migrate to the C forum

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: need urgent responce please!

 
0
  #4
Dec 1st, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: need urgent responce please!

 
1
  #5
Dec 2nd, 2008
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
Not to mention, the unindented code which is simply awful to look at.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: need urgent responce please!

 
0
  #6
Dec 2nd, 2008
Originally Posted by Salem View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC