Question: Improve my Appointment book code in the following ways: 1. When I enter a number, for example, lets say 2. I then enter my entries,
Improve my Appointment book code in the following ways:
1. When I enter a number, for example, lets say 2. I then enter my entries, and after that it's supposed to display the appointment and redisplay the menu and let me choose another selection. Instead, it goes straight to entering more entries instead of displaying the menu. So please fix my error, and have the menu display after each time I make a selection and enter the appropriate numbers.
2. Also, when fixed, write code that saves the appointments to a file.
#include #include #include #include
using namespace std;
const int DAYS_PER_MONTH = 30;
/** A class that describes a time of day (between 00:00:00 and 23:59:59) */ class Time { public: /** Constructs a time of day. @param hour the hours @param min the minutes @param sec the seconds */ Time(int hour, int min, int sec); /** Constructs a Time object that is set to the time at which the constructor executes. */ Time();
/** Gets the hours of this time. @return the hours */ int get_hours() const; /** Gets the minutes of this time. @return the minutes */ int get_minutes() const; /** Gets the seconds of this time. @return the seconds */ int get_seconds() const;
/** Computes the seconds between this time and another. @param t the other time @return the number of seconds between this time and t */ int seconds_from(Time t) const; /** Adds a number of seconds to this time. @param s the number of seconds to add */ void add_seconds(int s);
private: int time_in_secs; }; /** Computes the correct remainder for negative dividend @param a - an integer @param n - an integer > 0 @return the mathematically correct remainder r such that a - r is divisible by n and 0 <= r and r < n */ int remainder(int a, int n) { if (a >= 0) { return a % n; } else { return n - 1 - (-a - 1) % n; } }
Time::Time(int hour, int min, int sec) { time_in_secs = 60L * 60 * hour + 60 * min + sec; }
Time::Time() { time_in_secs = 0; }
int Time::get_hours() const { return time_in_secs / (60 * 60); }
int Time::get_minutes() const { return (time_in_secs / 60) % 60; }
int Time::get_seconds() const { return time_in_secs % 60; }
int Time::seconds_from(Time t) const { return time_in_secs - t.time_in_secs; }
void Time::add_seconds(int s) { const int SECONDS_PER_DAY = 60 * 60 * 24; time_in_secs = remainder(time_in_secs + s, SECONDS_PER_DAY); }
//..................................................................
class Date { public: Date(); Date(int y, int m, int d); void print() const; bool equals(Date other) const; private: int day; int month; int year; };
Date::Date() { day = 1; month = 1; year = 1; }
Date::Date(int y, int m, int d) { day = d; month = m; year = y; }
void Date::print() const { cout << year << "/" << month << "/" << day; }
bool Date::equals(Date other) const { return day == other.day && month == other.month && year == other.year; }
class Appointment { public: Appointment(); Appointment(string desc, Time s, Time e); void print() const; void read(); virtual bool occurs_on(int year, int month, int day) const; private: string description; Time start; Time end; };
Appointment::Appointment() { }
Appointment::Appointment(string desc, Time s, Time e) { description = desc; start = s; end = e; }
void Appointment::print() const { cout << " " << start.get_hours() << ":"; if (start.get_minutes() < 10) cout << "0"; cout << start.get_minutes() << " - " << end.get_hours() << ":"; if (end.get_minutes() < 10) cout << "0"; cout << end.get_minutes() << " " << description << " "; }
/** Determines if the appointment occurs on the given date. @param year a year value @param month a month value of 1 for Jan, to 12 for Dec @param day a day value @return true if the appointment occurs on the given date */ bool Appointment::occurs_on(int year, int month, int day) const { return false; }
class Onetime : public Appointment { public: Onetime(); Onetime(string desc, Date d, Time s, Time e); void read(); virtual bool occurs_on(int year, int month, int day) const; private: Date when; };
Onetime::Onetime() { }
Onetime::Onetime(string desc, Date d, Time s, Time e) : Appointment(desc, s, e) { when = d; }
/** Determines if the appointment occurs on the given date. @param year a year value @param month a month value of 1 for Jan, to 12 for Dec @param day a day value @return true if the appointment occurs on the given date */ bool Onetime::occurs_on(int year, int month, int day) const { return when.equals(Date(year, month, day)); }
class Daily : public Appointment { public: Daily(); Daily(string desc, Time s, Time e); virtual bool occurs_on(int year, int month, int day) const; };
Daily::Daily() { }
Daily::Daily(string desc, Time s, Time e) : Appointment(desc, s, e) { }
/** Determines if the appointment occurs on the given date. @param year a year value @param month a month value of 1 for Jan, to 12 for Dec @param day a day value @return true if the appointment occurs on the given date */ bool Daily::occurs_on(int year, int month, int day) const { return true; }
class Monthly : public Appointment { public: Monthly(); Monthly(string desc, int d, Time s, Time e); void read(); virtual bool occurs_on(int year, int month, int day) const; private: int day; };
Monthly::Monthly() { }
Monthly::Monthly(string desc, int d, Time s, Time e) : Appointment(desc, s, e) { day = d; }
bool Monthly::occurs_on(int year, int month, int d) const /** Determines if the appointment occurs on the given date. @param year a year value @param month a month value of 1 for Jan, to 12 for Dec @param day a day value @return true if the appointment occurs on the given date */
{ return day == d; }
int main() { int ch,ch1; string description; int sh, sm, eh, em; int year, month, day;
vector schedule(3);
do { cout << "1. One time appointment "; cout << "2. Daily appointment "; cout << "3. Monthly appointment "; cout << "4. exit "; cout << "Selection: "; cin >> ch; cin.ignore(); cout << endl;
switch(ch) { case 1: cout << "Enter the description: "; getline(cin, description); cout << "Enter the starting hour and minutes: "; cin >> sh >> sm; cout << "Enter the ending hour and minutes: "; cin >> eh >> em; cout << "Enter year: "; cin >> year; cout << "Enter month: "; cin >> month; cout << "Enter day: "; cin >> day;
schedule[0]= new Onetime(description, Date(year, month, day), Time(sh, sm, 0), Time(eh, em, 0)); cout << " On " << month << ", " << day << ", " << year << " you have these appointments at the following times: " << " "; schedule[0]->print(); cout << endl; case 2: cout << "Enter the description: "; getline(cin,description); cout << "Enter the starting hour and minutes: "; cin >> sh >> sm; cout << "Enter the ending hour and minutes: "; cin >> eh >> em; cout << "Enter year: "; cin >> year; cout << "Enter month: "; cin >> month; cout << "Enter day: "; cin >> day; schedule[1]= new Daily(description, Time(sh,sm,0), Time(eh, em, 0)); cout << " On " << month << ", " << day << ", " << year << " you have these appointments at the following times: " << " "; schedule[1]->print(); cout << endl;
case 3: cout << "Enter the description: "; getline(cin,description); cout << "Enter the starting hour and minutes: "; cin >> sh >> sm; cout << "Enter the ending hour and minutes: "; cin >> eh >> em; cout << "Enter year: "; cin >> year; cout << "Enter month: "; cin >> month; cout << "Enter day: "; cin >> day; schedule[2]= new Monthly(description,day, Time(sh,sm,0), Time(eh, em, 0)); cout << " On " << month << ", " << day << ", " << year << " you have these appointments at the following times: " << " "; schedule[2]->print(); cout << endl; case 4: break; }
}while(ch != 4); }
************Here is an example of the error:
1. One time appointment 2. Daily appointment 3. Monthly appointment 4. exit Selection: 2
Enter the description: Go to the park Enter the starting hour and minutes: 4 45 Enter the ending hour and minutes: 7 56 Enter year: 1992 Enter month: 2 Enter day: 3
On 2, 3, 1992 you have these appointments at the following times: 4:45 - 7:56 Go to the park
Enter the description: Enter the starting hour and minutes:
// After the appointment is displayed on the screen, it's supposed to allow me to make another selection from the menu. Instead it goes straight to description.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
