| | |
Day Of Year Problem
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 0
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class D { private: int d; static const int eom[]; static const string wk[]; static const string m[]; static const int dm[]; static const int lp[]; public: D(int da) { d = da; } static void print(int); }; const int D::eom[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; const int D::dm[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const string D::wk[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; const string D::m[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; const int D::lp[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; void D::print(int d) { int n = 0; int day = 0; int day2 = d; int day3 = 0; int mon = 0; int w = 0; int yr = 1900; int y = 0; int i = 0; int m1 = 0; cout << d / 365 << endl; cout << " and the day is " << d % 365 << endl; while(d > 365) { if (d < 0) { break; } d = d - 365; yr++; if (i == 3) { if (d < 0) { break; } d = d - 366; i = 0; yr++; } cout << yr << " Day " << d << endl; i++; } cout << "The year is: " << yr << endl << endl; if (yr % 4 == 0) { cout << yr << " is a leap year."; cout << endl; } else { cout << yr << " is not a leap year."; cout << endl; } cout << d << endl; if (d < 32) { cout << "January" << d; } else { i = 0; while (eom[mon] < d) { cout << "Day is: " << day << endl; day = d - eom[mon]; mon = (mon + 1) % 12; i++ - 1; } } cout << day; cout << "\nThe month is: " << m[i]; cout << endl; w = day2 % 7; cout << "\nThe day of the week is: " << wk[w]; cout << endl; cout << d << endl; cout << "\nThe day of the month is: " << day; cout << endl; } int main() { int num; cout << "This program will convert any number to\n"; cout << "Day Of Week/Month/Day/Year format.\n"; cout << endl; cout << "Enter any number: (ex. 32781)"; cout << endl; cin >> num; cout << endl; while(num < 0) { cout << "There are no negative dates in the history of the world.\n"; cout << "Please re-enter any number."; cin >> num; } cout << "The number " << num << " is also known as the date: \n"; cout << endl; D::print(num); cout << endl << endl; system("pause"); return 0; }
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Calendar problem (Python)
- problem with calendar.. (C++)
- Motherboard problem?..I think. (Motherboards, CPUs and RAM)
- Time zone problem (PHP)
- Access Date Problem (Computer Science)
- Scanf problem (C)
- Logic to Convert Days From 1800 to a Date (Month, Day, Year) (C++)
Other Threads in the C++ Forum
- Previous Thread: Print Star pattern Recursively
- Next Thread: Problem with Downcasting
Views: 758 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






