1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;
class dayOfYear {
private:
int userDay; // private class member to hold user input
string month;
public:
dayOfYear(); //Constructor
void setMonth(string); //set the month
string setUserMonth(); // set user month
void setDay(int); // Function to set day
int getDay()const; // Function to recieve day for later functions
void print() const; // Print() function
dayOfYear(string, int); //Constructor thats takes in 2 parameter...
};
dayOfYear::dayOfYear() : userDay(0) { } //Constructor initializing userDay to 0, no other initializations.
void dayOfYear::setMonth(string month) { this -> month = month; }
void dayOfYear::setDay(int userDay) { this -> userDay = userDay; } // Setting user input to userDay
int dayOfYear::getDay() const { return this -> userDay; } // Return this->userDay to calling function
dayOfYear::dayOfYear(string m_name, int m_day)
{
if (m_day > 0 && m_day < 31)
userDay = m_day;
month = m_name;
}
void dayOfYear::print() const { // Print out all relevant information
string months[] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i=1; i<= 12; i++)
{
if((month.compare(months[i]) == 0) && userDay <= days_in_month[i] && userDay > 0)
{//January
cout << month << " " << userDay << ": " << endl;// Print it out =)
return;
}
}
cout << "Invalid month or days in month.";
}
//Exactly as it sounds, get the user input of the day
int getUserDay() {
int number;
while(true) {
cout << "Please enter the day of the month: ";
cin >> number;
cin.ignore();
if(number > 0 && number < 366) {break; }
cout << "You entered an invalid number!!\n\n" << endl;
}
return number;
}
string getUserMonth() {
string month;
cout << "Please enter the month: ";
cin >> month;
while(true) {
if(month.compare("january") == 0 || month.compare("January") == 0) {
month = "January" ;
break;
}
if(month.compare("february") == 0 || month.compare("February") == 0) {
month = "February" ;
break;
}
if(month.compare("march") == 0 || month.compare("March") == 0) {
month = "March";
break;
}
if(month.compare("april") == 0 || month.compare("April") == 0) {
month = "April";
break;
}
if(month.compare("may") == 0 || month.compare("May") == 0) {
month = "May";
break;
}
if(month.compare("june") == 0 || month.compare("June") == 0) {
month = "June";
break;
}
if(month.compare("july") == 0 || month.compare("July") == 0) {
month = "July";
break;
}
if(month.compare("august") == 0 || month.compare("August") == 0) {
month = "August";
break;
}
if(month.compare("september") == 0 || month.compare("September") == 0) {
month = "September";
break;
}
if(month.compare("october") == 0 || month.compare("October") == 0) {
month = "October";
break;
}
if(month.compare("november") == 0 || month.compare("November") == 0) {
month = "November";
break;
}
if(month.compare("december") == 0 || month.compare("December") == 0) {
month = "December";
break;
}
cout << "You have misspelled the month " << month << " please re-enter! ";
cin >> month;
}
return month;
}
// Pass the class dayOfYear as printC() argument so it can access the members
void printC(dayOfYear &D) {
cout << endl;
// cout << D.getDay() << ": " ; // Get the day the user defined in the beginning
D.print(); // Print all the lovely information for the user =)
cout << "\n\n\n\n";
}
int main() {
dayOfYear D; // D represents dayOfYear class
D.setMonth(getUserMonth()); // Set the month by calling getUserMonth, than transferring info to setMonth
D.setDay(getUserDay()); // Set the day by calling getUserDay, than transferring to setDay to do so.
printC(D); // printC is passing the class dayOfYear which is D as it's argument so it can access the elements in the class
return 0;
}
|