Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year

Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help!

Code:

#include "calendarType.h" #include

using namespace std;

int main() { int month; int year; int SENTINEL = -9999; int userSent = 0;

while (userSent != SENTINEL) {

cout << "Please enter a month: "; cin >> month; cout << " ";

cout << "Please enter a year: "; cin >> year; cout << " ";

calendarType cal(month, year); //for 2014, first day for each month, //6 is sunday //9 is monday //4 tuesday //10 wednesday //5 is thursday //8 is friday //11 is saturday

cal.printCalendar();

cout << "If you would like to continue, type 0 and hit enter. " << " " << "If you would like to quit, type -9999 and hit enter. "; cin >> userSent; cout << " ";

}

system("pause");

return 0; }

calendarType.h /* * This class will create a calendar printout for any month and year after * January 1, 1500. */ #ifndef calendarType_h #define calendarType_h #include "dateType.h" #include "extDateType.h" #include "dayType.h" #include class calendarType { public: void setMonth(int m); void setYear(int y);

int getMonth(); int getYear();

void printCalendar();

calendarType(); calendarType(int m, int y);

private:

// Note that member functions can also be private which means only functions // within this class can call them. dayType firstDayOfMonth(); void printTitle(); void printDates();

// Composition rather than inheritance, although firstDate is a derived class // from the base class dateType. extDateType firstDate; dayType firstDay;

};

#endif

dateType.h

#ifndef dateType_H #define dateType_H class dateType { public: void setDate(int, int, int); void setMonth(int); void setDay(int); void setYear(int); void print() const;

int numberOfDaysPassed();

int numberOfDaysLeft();

void incrementDate(int nDays);

int getMonth() const; int getDay() const; int getYear() const;

int getDaysInMonth();

bool isLeapYear();

dateType(int = 1, int = 1, int = 1900);

private: int dMonth; int dDay; int dYear; };

#endif

dayType.h

#ifndef dayType_H #define dayType_H #include using namespace std;

class dayType { public: static string weekDays[7]; void print() const; string nextDay() const; string prevDay() const; void addDay(int nDays);

void setDay(string d); string getDay() const;

dayType(); dayType(string d);

private: string weekDay; };

#endif

extDateType.h

#ifndef extDateType_H #define extDateType_H #include

#include "dateType.h" using namespace std;

class extDateType: public dateType { public: static string eMonths[12]; void printLongDate(); void setDate(int, int, int); void setMonth(int m);

void printLongMonthYear();

extDateType(); extDateType(int, int, int);

private: string eMonth; };

#endif

dateTypeImp.cpp

#include #include "dateType.h" using namespace std;

void dateType::setDate(int month, int day, int year) { if (year >= 1) dYear = year; else dYear = 1900; if (1 <= month && month <= 12) dMonth = month; else dMonth = 1;

switch (dMonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (1 <= day && day <= 31) dDay = day; else dDay = 1; break; case 4: case 6: case 9: case 11: if (1 <= day && day <= 30) dDay = day; else dDay = 1; break; case 2: if (isLeapYear()) { if (1 <= day && day <= 29) dDay = day; else dDay = 1; } else { if (1 <= day && day <= 28) dDay = day; else dDay = 1; } } }

void dateType::setMonth(int m) { dMonth = m; }

void dateType::setDay(int d) { dDay = d; }

void dateType::setYear(int y) { dYear = y; }

void dateType::print() const { cout << dMonth << "-" << dDay << "-" << dYear; }

int dateType::getMonth() const { return dMonth; }

int dateType::getDay() const { return dDay; }

int dateType::getYear() const { return dYear; }

int dateType::getDaysInMonth() { int noOfDays;

switch (dMonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: noOfDays = 31; break; case 4: case 6: case 9: case 11: noOfDays = 30; break; case 2: if (isLeapYear()) noOfDays = 29; else noOfDays = 28; }

return noOfDays; }

bool dateType::isLeapYear() { if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 == 0) return true; else return false; }

dateType::dateType(int month, int day, int year) { setDate(month, day, year); }

int dateType::numberOfDaysPassed() { int monthArr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int sumDays = 0; int i;

for (i = 1; i < dMonth; i++) sumDays = sumDays + monthArr[i];

if (isLeapYear() && dMonth > 2) sumDays = sumDays + dDay + 1; else sumDays = sumDays + dDay;

return sumDays; }

int dateType::numberOfDaysLeft() { if (isLeapYear()) return 366 - numberOfDaysPassed(); else return 365 - numberOfDaysPassed(); }

void dateType::incrementDate(int nDays) { int monthArr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int daysLeftInMonth;

daysLeftInMonth = monthArr[dMonth] - dDay;

if (daysLeftInMonth >= nDays) dDay = dDay + nDays; else { dDay = 1; dMonth++; nDays = nDays - (daysLeftInMonth + 1);

while (nDays > 0) if (nDays >= monthArr[dMonth]) { nDays = nDays - monthArr[dMonth];

if ((dMonth == 2) && isLeapYear()) nDays--; dMonth++; if (dMonth > 12) { dMonth = 1; dYear++; }

} else { dDay = dDay+nDays; nDays = 0; } } }

dayTypeImp.cpp

#include #include #include "dayType.h" using namespace std;

string dayType::weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; void dayType::print() const { cout << weekDay; }

string dayType::nextDay() const { int i;

for (i = 0; i < 7; i++) if (weekDays[i] == weekDay) break; return weekDays[(i + 1) % 7]; }

string dayType::prevDay() const { if (weekDay == "Sunday") return "Saturday"; else { int i;

for (i = 0; i < 7; i++) if (weekDays[i] == weekDay) break; return weekDays[i - 1]; } }

void dayType::addDay(int nDays) { int i;

for (i = 0; i < 7; i++) if (weekDays[i] == weekDay) break; weekDay = weekDays[(i + nDays) % 7]; }

void dayType::setDay(string d) { weekDay = d; }

string dayType::getDay() const { return weekDay; }

dayType::dayType() { weekDay = "Sunday"; }

dayType::dayType(string d) { weekDay = d; }

extDateTypeImp.cpp

#include #include #include "dateType.h" #include "extDateType.h" using namespace std; string extDateType::eMonths[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void extDateType::printLongDate() { cout << eMonth << " " << getDay() << ", " << getYear(); }

void extDateType::printLongMonthYear() { cout << eMonth << " " << getYear(); }

void extDateType::setDate(int m, int d, int y) { dateType::setDate(m, d, y);

eMonth = eMonths[m - 1]; }

void extDateType::setMonth(int m) { dateType::setMonth(m); eMonth = eMonths[m - 1]; }

extDateType::extDateType() { eMonth = "January"; }

extDateType::extDateType(int m, int n, int d) : dateType(m, n, d) { eMonth = eMonths[m - 1]; }

calendarTypeImp.cpp

#include "calendarType.h"

//defaults constructor to January 1, 1500 calendarType::calendarType() { setMonth(1); setYear(1500); }

//sets input month and year calendarType::calendarType(int m, int y) { setMonth(m); setYear(y); }

void calendarType::setMonth(int m) { firstDate.setMonth(m); }

void calendarType::setYear(int y) { firstDate.setYear(y); }

int calendarType::getMonth() { return firstDate.getMonth(); }

int calendarType::getYear() { return firstDate.getYear(); }

void calendarType::printCalendar() { printTitle(); printDates(); }

void calendarType::printTitle() { firstDate.printLongMonthYear();

cout << " ";

for (int i = 0; i != 8; i++) { string shortName = firstDay.weekDays[i].substr(0, 3);

cout << " " << shortName; } }

void calendarType::printDates() {

int i = 0; dayType day = firstDayOfMonth();

if (day.getDay() == "Sunday") {

while (firstDate.getDay() < firstDate.getDaysInMonth()) { i = 0; cout << " ";

while (i < 7) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); else cout << " " << " " << firstDate.getDay();

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; } } cout << " "; }

if (day.getDay() == "Monday")

{ cout << " ";

for (int k = 0; k < 1; k++) { cout << " " << " "; }

while (i < 6) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; }

while (firstDate.getDay() < firstDate.getDaysInMonth()) { i = 0; cout << " ";

while (i < 7) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; } } cout << " ";

}

if (day.getDay() == "Tuesday")

{

cout << " ";

for (int k = 0; k < 2; k++) { cout << " " << " "; }

while (i < 5) //the literal is decremented as day of week moves further right in columns { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay();

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; }

//copypaste loop while (firstDate.getDay() < firstDate.getDaysInMonth()) { i = 0; cout << " ";

while (i < 7) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); else cout << " " << " " << firstDate.getDay();

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; } } cout << " ";

}

if (day.getDay() == "Wednesday")

{

cout << " ";

for (int k = 0; k < 3; k++) { cout << " " << " "; }

while (i < 4) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; }

while (firstDate.getDay() < firstDate.getDaysInMonth()) { i = 0; cout << " ";

while (i < 7) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; } } cout << " ";

}

if (day.getDay() == "Thursday")

{

cout << " ";

for (int k = 0; k < 4; k++) { cout << " " << " "; }

while (i < 3) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); else cout << " " << " " << firstDate.getDay();

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; }

while (firstDate.getDay() < firstDate.getDaysInMonth()) { i = 0; cout << " ";

while (i < 7) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); else cout << " " << " " << firstDate.getDay();

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; } } cout << " ";

}

if (day.getDay() == "Friday")

{

cout << " ";

for (int k = 0; k < 5; k++) { cout << " " << " "; }

while (i < 2) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; }

while (firstDate.getDay() < firstDate.getDaysInMonth()) { i = 0; cout << " ";

while (i < 7) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; } } cout << " ";

}

if (day.getDay() == "Saturday")

{

cout << " ";

for (int k = 0; k < 6; k++) { cout << " " << " "; }

while (i < 1) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; }

while (firstDate.getDay() < firstDate.getDaysInMonth()) { i = 0; cout << " ";

while (i < 7) { if (firstDate.getDay() <= 9) cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters else cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day

if (firstDate.getDay() < firstDate.getDaysInMonth()) { firstDate.incrementDate(1); }

i++; } } cout << " ";

}

}

dayType calendarType::firstDayOfMonth() { dayType day; int countResult; int y = getYear(); int m = getMonth(); int d = 1;

if (firstDate.getMonth() == 1 && firstDate.getYear() == 1500) { day.setDay(firstDay.weekDays[1]); } else { static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; y -= m < 3; countResult = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;

day.setDay(firstDay.weekDays[countResult]); }

return day; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions