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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
char inputChar(string prompt, string listChars);
int inputInteger(string prompt, int startRange, int endRange);
class Month
{
private:
int month;
char letter1,
letter2,
letter3;
public:
Month();
Month(int month);
Month(char letter1, char letter2, char letter3);
void setMonth(int month);
int getMonthInt()const;
void setMonth(char letter1, char letter2, char letter3);
string getMonthName()const;
Month getNextMonth()const;
void input(istream& in, Month &monthInteger, Month &monthLetter);
void output(ostream&, Month &monthInteger, Month &monthLetter);
};
Month::Month() : month(1) {};
Month::Month(int month)
{
this->month = month;
}
Month::Month(char letter1, char letter2, char letter3)
{
if ((letter1 == 'j') && (letter2 == 'a') && (letter3 == 'n'))
month = 1;
if ((letter1 == 'f') && (letter2 == 'e') && (letter3 == 'b'))
month = 2;
if ((letter1 == 'm') && (letter2 == 'a') && (letter3 == 'r'))
month = 3;
if ((letter1 == 'a') && (letter2 == 'p') && (letter3 == 'r'))
month = 4;
if ((letter1 == 'm') && (letter2 == 'a') && (letter3 == 'y'))
month = 5;
if ((letter1 == 'j') && (letter2 == 'u') && (letter3 == 'n'))
month = 6;
if ((letter1 == 'j') && (letter2 == 'u') && (letter3 == 'l'))
month = 7;
if ((letter1 == 'a') && (letter2 == 'u') && (letter3 == 'g'))
month = 8;
if ((letter1 == 's') && (letter2 == 'e') && (letter3 == 'p'))
month = 9;
if ((letter1 == 'o') && (letter2 == 'c') && (letter3 == 't'))
month = 10;
if ((letter1 == 'n') && (letter2 == 'o') && (letter3 == 'v'))
month = 11;
if ((letter1 == 'd') && (letter2 == 'e') && (letter3 == 'c'))
month = 12;
}
void Month::setMonth(int month)
{
month >= 1 && month <= 12 ? this->month = month : this->month = 0;
}
int Month::getMonthInt()const
{
return month;
}
void Month::setMonth(char letter1, char letter2, char letter3)
{
if ((letter1 == 'j') && (letter2 == 'a') && (letter3 == 'n'))
month = 1;
if ((letter1 == 'f') && (letter2 == 'e') && (letter3 == 'b'))
month = 2;
if ((letter1 == 'm') && (letter2 == 'a') && (letter3 == 'r'))
month = 3;
if ((letter1 == 'a') && (letter2 == 'p') && (letter3 == 'r'))
month = 4;
if ((letter1 == 'm') && (letter2 == 'a') && (letter3 == 'y'))
month = 5;
if ((letter1 == 'j') && (letter2 == 'u') && (letter3 == 'n'))
month = 6;
if ((letter1 == 'j') && (letter2 == 'u') && (letter3 == 'l'))
month = 7;
if ((letter1 == 'a') && (letter2 == 'u') && (letter3 == 'g'))
month = 8;
if ((letter1 == 's') && (letter2 == 'e') && (letter3 == 'p'))
month = 9;
if ((letter1 == 'o') && (letter2 == 'c') && (letter3 == 't'))
month = 10;
if ((letter1 == 'n') && (letter2 == 'o') && (letter3 == 'v'))
month = 11;
if ((letter1 == 'd') && (letter2 == 'e') && (letter3 == 'c'))
month = 12;
}
string Month::getMonthName()const
{
switch (this->month)
{
case 1:
return "Jan";
break;
case 2:
return"Feb";
break;
case 3:
return "Mar";
break;
case 4:
return "Apr";
break;
case 5:
return "May";
break;
case 6:
return "Jun";
break;
case 7:
return "Jul";
break;
case 8:
return "Aug";
break;
case 9:
return "Sep";
break;
case 10:
return "Oct";
break;
case 11:
return "Nov";
break;
case 12:
return "Dec";
break;
default:
return "Error - the month is not valid!";
}
}
Month Month::getNextMonth()const
{
if (month == 12) return Month(1);
else return Month(month + 1);
}
void Month::input(istream& in, Month &monthInteger, Month &monthLetter)
{
int monthInt = inputInteger("Enter month's number: ", 1, 12);
//Month monthInteger;
monthInteger.setMonth(monthInt);
char letter1, letter2, letter3;
letter1 = inputChar("Enter first letter of month: ", "JANFEBMRPLYUGSOCTVD");
letter2 = inputChar("Enter second letter of month: ", "JANFEBMRPLYUGSOCTVD");
letter3 = inputChar("Enter third letter of month: ", "JANFEBMRPLYUGSOCTVD");
//Month monthLetter;
monthLetter.setMonth(letter1, letter2, letter3);
}
void Month::output(ostream& out, Month &monthInteger, Month &monthLetter)
{
//Month monthInteger;
cout << "Month input: " << monthInteger.getMonthName() << endl;
Month outNextInt = monthInteger.getNextMonth();
cout << "Next month: " << outNextInt.getMonthInt() << " " << outNextInt.getMonthName();
cout << endl << endl;
//Month monthLetter;
cout << "Month input: " << monthLetter.getMonthName() << endl;
Month outNextLetter = monthLetter.getNextMonth();
cout << "Next month: " << outNextLetter.getMonthInt() << " " << outNextLetter.getMonthName();
}
int main()
{
Month monthInteger;
Month monthLetter;
Month monthProj;
monthProj.input(cin, monthInteger, monthLetter);
monthProj.output(cout, monthInteger, monthLetter);
//below is an example of how the input and output member functions are supposed to do...
/*
int monthInt = inputInteger("Enter month's number: ", 1, 12); //1a
Month monthInteger;
monthInteger.setMonth(monthInt);
cout << "Month input: " << monthInteger.getMonthName() << endl; //1b
Month outNextInt = monthInteger.getNextMonth();
cout << "Next month: " << outNextInt.getMonthInt() << " " << outNextInt.getMonthName();
cout << endl << endl;
char letter1, letter2, letter3; //2a
letter1 = inputChar("Enter first letter of month: ", "JANFEBMRPLYUGSOCTVD");
letter2 = inputChar("Enter second letter of month: ", "JANFEBMRPLYUGSOCTVD");
letter3 = inputChar("Enter third letter of month: ", "JANFEBMRPLYUGSOCTVD");
Month monthLetter;
monthLetter.setMonth(letter1, letter2, letter3);
cout << "Month input: " << monthLetter.getMonthName() << endl; //2b
Month outNextLetter = monthLetter.getNextMonth();
cout << "Next month: " << outNextLetter.getMonthInt() << " " << outNextLetter.getMonthName();
*/
cout << endl << endl;
}
//just some validation function
char inputChar(string prompt, string listChars)
{
char input;
do
{
cout << prompt;
if (!(cin >> input))
{
cout << "ERROR-1A: Invalid input. Must be a character type.\n";
cin.clear();
cin.ignore(999, '\n');
}
bool bfound = false;
for (unsigned c = 0; c < listChars.length(); c++)
if (toupper(listChars[c]) == toupper(input))
{
bfound = true;
break;
}
if (!bfound)
{
cout << "ERROR-2A: Invalid input. Must be a character from the list of '";
for (unsigned c = 0; c < listChars.length() - 1; c++)
cout << "'" << static_cast<char>(toupper(listChars[c])) << "', ";
cout << "or '" << static_cast<char>(toupper(listChars.back())) << "'.\n";
}
else
break;
} while (true);
return input;
}
int inputInteger(string prompt, int startRange, int endRange)
{
int input;
do
{
cout << prompt;
if (!(cin >> input))
{
cout << "ERROR-3A: Invalid input. Must be an integer type.\n";
cin.clear();
cin.ignore(999, '\n');
}
else if (!((input >= 1) && (input <= 12)))
cout << "ERROR-3A: Invalid input. Must be from " << startRange << "..." << endRange << ".\n";
else
break;
} while (true);
return input;
}
|