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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// error message
template<typename T> // useful for getting user's keyboard input without crash or hang
T kbd_input(const string &prompt="Enter: ", const string &errmsg=" Oops! Try again.\n\n") {
while (true) {
cout << prompt; T ans{};
if (cin>>ans) return ans;
else {
cin.clear(); cin.ignore(INT_MAX, '\n'); cout<<errmsg;
}
}
}
const int NUM_MONTHS = 13;
// class
class Day {
private:
int day_of_year; // To hold a day
string month; // To hold a month name
int arrayIdx; // Stores an array index
public:
// month:
static const int daysInMonth[12];
static const int daysInMonth_accum[12];
static const string monthsOfYear[NUM_MONTHS];
enum month_t {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
const static int daysInYear[NUM_MONTHS];
// week
static const string daysOfWeek[7];
enum weekday_t {sun, mon, tue, wed, thr, fri, sat};
// day
static const int BASE_YEAR=2021;
static const month_t BASE_MONTH=jan;
static const weekday_t BASE_WEEKDAY=fri; // January 1, 2021 was a Friday
static const int BASE_DAY=1;
explicit Day( int ndays = 1 ) noexcept ; // constructor
// implement 3 constructors, ++day, day++, --day, day--; "show", "print", "display", << as needed
Day operator++(int);
Day operator--(int);
Day operator++();
Day operator--();
// Members can be here (inline) or after.
Day(int d)
{
day_of_year = d;
}
void print();
};
// initialized monthsOfYear, daysOfWeek, daysInMonth, daysInMonth_accum
const string Day::monthsOfYear[] = {"january", "february",
"march", "april", "may", "june", "july", "august", "september", "october", "november",
"december"};
const string Day::daysOfWeek[7] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
const int Day::daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int Day::daysInMonth_accum[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
// limits day to positive numbers after 1 / no error message
Day::Day( int ndays ) noexcept:day_of_year( std::min( 1, ndays ) ) {}
void Day::print()
{
bool isFound = false;
for (int count = 0; count < NUM_MONTHS && isFound != true; count++)
{
if (day_of_year <= daysInYear[count])
{
cout << monthsOfYear[count] << " " << (day_of_year %= daysInYear[count-1]);
isFound = true;
}
}
}
int main() {
int number = 0;
cout <<"Welcome to ????'s calendar helper!\n";
Day day; // this is updated in the set day options, and is the 1st day displayed in the Display options.
for (bool running=true; running; ) {
cout<<"\nOptions: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: ";
string option; char option_letter; cin>>option; option_letter=tolower(option[0]);
switch (option_letter) {
case 's': // set day, test constructors: Day(), Day(int) constructor
{
char option = tolower(kbd_input<char>(" set day d)efault m)month,day n)umber v)ary: "));
if (option=='q') break;
switch(option) {
case 'd': {
cout << "enter a day number (day#=[1]): ";
// getline(cin, monthName);
// day 1 = Jan 1 2021
// day 366 = jan 1, 2022
break;
}
case 'm': {
cout << "enter a month (3-letter-month, like: jan): ";
break;
}
case 'n': {
cout <<"enter a day of the year (1-365): ";
// can only change number of day
// this only changes the day within the year
// cannot go into next year
break;
}
case 'v': {
/*
cout <<"the current day is : "<< 500;
cout<< "\n+/- number to vary current day: ";
cin<< // if they enter -100 remove 100 days from current day.
cout <<"\ncurrent day is now: ";
cout <<"\nday#=["<<865<<"]"<< mon may 15, 2023;
// subtracts day from current day. day can be over 365
//cannot subtract 366 from 365 */
break;
}
default:
cout<<"Invalid option, try again. q to quit."; break;// continue;
}
// cout<<" day#=["<<day<<"] "<<day.to_string()<<endl; // generic output. Could be helpful.
break;
} // case 's' for set day
case 'd': {
cout<<"You chose to display the current day...\n";
cout << "Please enter a day (1 through 365): ";
cin >> number;
while (number <= 0 || number > 365)
{
cout << "Please enter a day (1 through 365): ";
cin >> number;
}
// Create a DayOfYear class object
Day day_of_year(number);
day_of_year();
break;
}
case 'm': {
cout<<"You chose to display the current month...\n";
break;
}
case 'w': {
cout<<"You chose to display the current week...\n";
break;
}
case 't': {
cout<<"t option - test increment, decrement operators, TO BE IMPLMENTED\n";
break;
}
case 'q':
running=false;
break;
default:
cout<<" Invalid option, try again. q to quit.\n";
break;
}
}
cout<<"\nThanks for using ??????'s calendar helper!\nGood-bye!\n";
}
| |