Question

Hello, I have a bug in my code, and when I run it should ask the...

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 <iostream>

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 << "\n";

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

       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. " << "\n" << "If you would like to quit, type -9999 and hit enter. ";
       cin >> userSent;
       cout << "\n";

   }

   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 <iostream>

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 <string>

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 <string>

#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 <iostream>
#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 <iostream>
#include <string>

#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 <iostream>
#include <string>
#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 << "\n";

   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 << "\n";


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


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

               i++;
           }
       }
       cout << "\n\n";
   }


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

   {
cout << "\n";

       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 << "\n";


           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 << "\n\n";

   }

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

   {

       cout << "\n";

           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 << "\n";


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


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

               i++;
           }
       }
       cout << "\n\n";

   }

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

   {

  

       cout << "\n";

       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 << "\n";


           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 << "\n\n";

   }

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

   {

cout << "\n";

       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 << "\n";


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


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

               i++;
           }
       }
       cout << "\n\n";

   }

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

   {

cout << "\n";

       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 << "\n";


           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 << "\n\n";

   }

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

   {

       cout << "\n";

       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 << "\n";


           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 << "\n\n";

   }

}

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your code is working fine. Maybe you are compiling it wrong. I ran the compiling command - g++ -o main main.cpp calendarTypeImp.cpp dateTypeImp.cpp extDateTypeImp.cpp dayTypeImp.cpp. Then I ran the programme using - ./main. The attached image shows the input and output of the programme.

[Yashs-MacBook-Pro:Assignment 1 yashgautam$ ./maitn Please enter a month: 7 Please enter a year 2004 July 2004 Fri 2 Sun Mon

Add a comment
Know the answer?
Add Answer to:
Hello, I have a bug in my code, and when I run it should ask the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

  • Can someone please help me. i keep getting an error in my code. I need it...

    Can someone please help me. i keep getting an error in my code. I need it to be on three seperate files! attached is my assignment and my code. c++ The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

    C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> 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 /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • for the following code I need the source code and output screenshot (includes date/time) in a...

    for the following code I need the source code and output screenshot (includes date/time) in a PDF format. I keep getting an error for the #include "dayType.hpp" dayType.cpp #include"dayType.hpp" string dayType::weekday[7] = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" }; //set the day void dayType::setDay(string day){ cout << "Set day to: " ; cin >> dayType::day; for(int i=0; i<7; i++) { if(dayType::weekday[i]==dayType::day) { dayType::markDay = i; } } } //print the day void dayType::printDay() { cout << "Day = "...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT