Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- name: Stefan Arambasich
- class : CIS 150
- due: 12/10
- description: This program is an implementation of a date class and will perform various functions such as setting the date,
- comparing two dates, and advancing the date one day at a time.
- */
- #include <iostream>
- using namespace std;
- class date
- {
- private:
- //Declarations
- int month, day, year;
- public:
- //Constructors
- date ();
- date (int m, int d, int y);
- //Member function prototypes
- int compareDates (date);
- void setDate (int, int, int);
- void increment ();
- int getDay ();
- int getMonth();
- int getYear();
- };
- int main ()
- {
- //Declare our variables
- int userDay, userMonth, userYear, compareDay, compareMonth, compareYear, day, month, year, when;
- date date1, date2, test;
- char selection;
- //Instead of leaving the user with more numbers than he/she needs, this array converts unclear numbers into specific names
- char monthName[13][20] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- //Test the 3 parameter constructor
- test.setDate(3, 1, 1900);
- day = test.getDay();
- month = test.getMonth();
- year = test.getYear();
- cout << "Testing the 3 parameter constructor" << endl << day << " " << month << " " << year << endl;
- //The loop affords the user opportunity to use the menu/program as many times as he/she pleases.
- while (selection != 'Q')
- {
- cout << "Menu" << endl
- << "1. Compare two dates" << endl
- << "2. Advance the day by one" << endl
- << "3. Get the date" << endl
- << "4. Get the month" << endl
- << "5. Get the year" << endl
- << "6. Set the date" << endl
- << "Enter your selection below followed by the return key or enter. Or type 'Q' (uppercase) to quit the program.\n\n";
- cin >> selection;
- switch (selection)
- {
- case '1':
- // DO STUFF HERE
- break;
- case '2':
- // DO OTHER STUFF HERE
- break;
- case '3':
- // DO OTHER STUFF HERE
- break;
- case '4':
- // DO OTHER STUFF HERE
- break;
- case '5':
- // DO OTHER STUFF HERE
- break;
- case '6':
- // DO OTHER STUFF HERE
- break;
- default:
- cout << "Invalid selection. Try again\n";
- break;
- }
- }
- }
- //Regular functions
- int checkMonthLength(int &day, int &month, int &year)
- {
- //Define what the length of each month is
- int lengthsOfMonths[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- //Check leap year
- if (day == 28 && month == 2 && (year % 4) == 0)
- day++;
- else if (day < lengthsOfMonths[month])
- day++;
- else if ((day + 1) > lengthsOfMonths[month] && month != 12)
- {
- day = 1;
- month++;
- }
- else if (month == 12)
- {
- day = 1;
- month = 1;
- year++;
- }
- //Compiler complains if I don't have this for some reason.
- return 0;
- }
- //Member functions
- int date::getDay ()
- {
- return day;
- }
- int date::getMonth()
- {
- return month;
- }
- int date::getYear ()
- {
- return year;
- }
- void date::increment ()
- {
- //Increment the day regardless if it's less than 28. If it's 28, we need to check for Feb.
- if (day < 28)
- day++;
- else
- checkMonthLength(day, month, year);
- }
- int date::compareDates (date x)
- {
- /* If the day entered is the same, return 0. After that, this process goes from broad (years) to more specific (days) to analyze whether a user's input
- occurs before or after the date set by the constuctor or by case 6 in the original switch statement */
- if (year < x.year)
- return -1;
- else if (year > x.year)
- return 1;
- else if (year == x.year)
- {
- if (month < x.month)
- return -1;
- else if (month > x.month)
- return 1;
- else if (month == x.month)
- {
- if (day < x.day)
- return -1;
- else if (day > x.day)
- return 1;
- }
- }
- return 0;
- }
- //Since the constructor cannot be called upon, we must write a separate function to re-set the date.
- void date::setDate (int userDay, int userMonth, int userYear)
- {
- month = userMonth;
- day = userDay;
- year = userYear;
- }
- //Sets the date to 1/1/1900 initally
- date::date()
- {
- month = 1;
- day = 1;
- year = 1900;
- }
- //3 parameter constructor. Unfortunately serves no purpose in this program, but I like full credit, so I will add it in here.
- date::date (int m, int d, int y)
- {
- month = m;
- day = d;
- year = y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement