Day Of Year Problem

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Day Of Year Problem

 
0
  #1
May 7th, 2009
Thanks in advance for your help. My code is doing some weird things and I can't figure out why. The purpose of my program is to have the user input any number and get the day of the week, month, day, and year and if that year is a leap year or not starting at the year 1900. If it is a leap year obviously there will be a difference in output for the month of the Feb. For example, if the user inputs 1, the output should be Sunday, January 1, 1900. This year was a leap year (I don't think it actually is, but it will be for the purposes of this program). Here is my code:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class D
  5. {
  6. private:
  7. int d;
  8. static const int eom[];
  9. static const string wk[];
  10. static const string m[];
  11. static const int dm[];
  12. static const int lp[];
  13. public:
  14. D(int da)
  15. {
  16. d = da;
  17. }
  18. static void print(int);
  19. };
  20.  
  21. const int D::eom[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
  22. const int D::dm[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  23. const string D::wk[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
  24. const string D::m[] = {"January", "February", "March", "April", "May", "June", "July", "August",
  25. "September", "October", "November", "December"};
  26. const int D::lp[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  27.  
  28. void D::print(int d)
  29. {
  30. int n = 0;
  31. int day = 0;
  32. int day2 = d;
  33. int day3 = 0;
  34. int mon = 0;
  35. int w = 0;
  36. int yr = 1900;
  37. int y = 0;
  38. int i = 0;
  39. int m1 = 0;
  40.  
  41. cout << d / 365 << endl;
  42. cout << " and the day is " << d % 365 << endl;
  43. while(d > 365)
  44. {
  45. if (d < 0)
  46. {
  47. break;
  48. }
  49. d = d - 365;
  50. yr++;
  51. if (i == 3)
  52. {
  53. if (d < 0)
  54. {
  55. break;
  56. }
  57. d = d - 366;
  58. i = 0;
  59. yr++;
  60. }
  61. cout << yr << " Day " << d << endl;
  62. i++;
  63. }
  64.  
  65. cout << "The year is: " << yr << endl << endl;
  66.  
  67. if (yr % 4 == 0)
  68. {
  69. cout << yr << " is a leap year.";
  70. cout << endl;
  71. }
  72. else
  73. {
  74. cout << yr << " is not a leap year.";
  75. cout << endl;
  76. }
  77.  
  78. cout << d << endl;
  79. if (d < 32)
  80. {
  81. cout << "January" << d;
  82. }
  83. else
  84. {
  85. i = 0;
  86. while (eom[mon] < d)
  87. {
  88. cout << "Day is: " << day << endl;
  89. day = d - eom[mon];
  90. mon = (mon + 1) % 12;
  91. i++ - 1;
  92. }
  93. }
  94. cout << day;
  95. cout << "\nThe month is: " << m[i];
  96. cout << endl;
  97.  
  98. w = day2 % 7;
  99.  
  100. cout << "\nThe day of the week is: " << wk[w];
  101. cout << endl;
  102. cout << d << endl;
  103.  
  104. cout << "\nThe day of the month is: " << day;
  105. cout << endl;
  106. }
  107.  
  108. int main()
  109. {
  110. int num;
  111.  
  112. cout << "This program will convert any number to\n";
  113. cout << "Day Of Week/Month/Day/Year format.\n";
  114. cout << endl;
  115. cout << "Enter any number: (ex. 32781)";
  116. cout << endl;
  117. cin >> num;
  118. cout << endl;
  119.  
  120. while(num < 0)
  121. {
  122. cout << "There are no negative dates in the history of the world.\n";
  123. cout << "Please re-enter any number.";
  124. cin >> num;
  125. }
  126.  
  127. cout << "The number " << num << " is also known as the date: \n";
  128. cout << endl;
  129. D::print(num);
  130. cout << endl << endl;
  131.  
  132. system("pause");
  133. return 0;
  134. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,133
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: Day Of Year Problem

 
0
  #2
May 7th, 2009
You didn't tell us the most important thing about your program. What is happening to make you think it's weird?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Day Of Year Problem

 
0
  #3
May 7th, 2009
Well...sometimes the month will display the right month, other times it will be off by one. Also, the day is off by one. It works for some and not for others, then other times it doesn't work at all. :-)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,133
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: Day Of Year Problem

 
0
  #4
May 7th, 2009
First thing you need to do is rethink the YEAR loop. Make it shorter. The leap year section in it makes the loop too complicated, and it's wrong. Don't deal with LY as a full year, it's one day.

Start with that.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Day Of Year Problem

 
0
  #5
May 11th, 2009
Thanks WaltP. I got it figured out.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 758 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC