Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- const string DAYS = "UMTWRFS";
- class Month
- {
- private:
- int numDays;
- int startDOW;
- public:
- Month(int days, char day1){this->numDays = days; this->startDOW = string("UMTWRFS").find(day1);};
- friend ostream & operator<<(ostream & out, Month m){
- cout << "Su Mo Tu We Th Fr Sa" << endl;
- int prints = 7;
- for (int i = 0;i < m.startDOW;i++, prints++) cout << " ";
- for (int day = 1;day <= m.numDays; day++, prints++){
- if (!(prints % 7)) cout << endl;
- cout << setw(2) << day;
- cout << ' ';
- }
- }
- };
- int main()
- {
- int year, month;
- char startDay;
- do {
- cout << "Please enter the year: ";
- cin >> year;
- } while ((year < 1) || (year > 9999));
- do {
- cout << "Please enter the month (1-12): ";
- cin >> month;
- } while ((month < 1) || (month > 12));
- do {
- cout << "Please enter the day of week that the year begins with (U,M,T,W,R,F,S): ";
- cin >> startDay;
- } while (DAYS.find(startDay) == string::npos);
- int lengths[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int totalDays = 0;
- for (int i = 0; i < month; i++) totalDays += lengths[i];
- if ((month > 2) && (!(year % 4) || (!(year % 100) && !(year % 400)))) totalDays++; // leap year, and yes, there probably are too many parens
- Month cal(lengths[month], ((DAYS.find(startDay)+totalDays) % 7)+1); // not 100% sure abt the math for the second arg
- cout << "\n\n\n" << cal << endl; // original string to make it look nice.
- return 0;
- }
Add Comment
Please, Sign In to add comment