generated from Auburn-University-COMP-3000/Assignment_4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq4.cpp
210 lines (188 loc) · 4.85 KB
/
q4.cpp
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
#include <iostream>
#include <cstdlib> // for exit()
#include <cctype> // for tolower()
using namespace std;
class Month
{
public:
Month(char c1, char c2, char c3); //constructor to set month based on first 3 chars of the month name
Month( int monthNumber); //a constructor to set month base on month number, 1 = January etc.
Month(); //a default constructor (what does it do? nothing)
void getMonthByNumber(istream& in); //an input function to set the month based on the month number
void getMonthByName(istream& in); //input function to set the month based on a three character input
void outputMonthNumber(ostream& out); //an output function that outputs the month as an integer,
void outputMonthName(ostream& out); //an output function that outputs the month as the letters.
Month nextMonth(); //a function that returns the next month as a month object
int monthNumber();
private:
int monthNum;
};
int main(){
// Testing constuctor from chars
cout << "Should output information for January:" << endl;
Month m('j','a','n');
m.outputMonthName(cout);
cout << " ";
m.outputMonthNumber(cout);
cout << endl;
// Testing constuctor from int
cout << "Should output information for February:" << endl;
Month n(2);
n.outputMonthName(cout);
cout << " ";
n.outputMonthNumber(cout);
cout << endl;
// Testing next month
cout << "Should output information for February:" << endl;
m = m.nextMonth();
m.outputMonthName(cout);
cout << " ";
m.outputMonthNumber(cout);
cout << endl;
// Testing Input
cout << "Input first three letters of a month:" << endl;
m.getMonthByName(cin);
m.outputMonthName(cout);
cout << " ";
m.outputMonthNumber(cout);
cout << endl;
// Testing Input
cout << "Input the number of a month:" << endl;
m.getMonthByNumber(cin);
m.outputMonthName(cout);
cout << " ";
m.outputMonthNumber(cout);
cout << endl;
}
Month::Month(char c1, char c2, char c3){
c1 = tolower(c1);
c2 = tolower(c2);
c3 = tolower(c3);
if (c1 == 'j' && c2 == 'a' && c3 == 'n'){
monthNum = 1;
} else if (c1 == 'f' && c2 == 'e' && c3 == 'b'){
monthNum = 2;
} else if (c1 == 'm' && c2 == 'a' && c3 == 'r'){
monthNum = 3;
} else if (c1 == 'a' && c2 == 'p' && c3 == 'r'){
monthNum = 4;
} else if (c1 == 'm' && c2 == 'a' && c3 == 'y'){
monthNum = 5;
} else if (c1 == 'j' && c2 == 'u' && c3 == 'n'){
monthNum = 6;
} else if (c1 == 'j' && c2 == 'u' && c3 == 'l'){
monthNum = 7;
} else if (c1 == 'a' && c2 == 'u' && c3 == 'g'){
monthNum = 8;
} else if (c1 == 's' && c2 == 'e' && c3 == 'p'){
monthNum = 9;
} else if (c1 == 'o' && c2 == 'c' && c3 == 't'){
monthNum = 10;
} else if (c1 == 'n' && c2 == 'o' && c3 == 'v'){
monthNum = 11;
} else if (c1 == 'd' && c2 == 'e' && c3 == 'c'){
monthNum = 12;
} else {
cout << "Sorry that is not a month";
exit(1);
}
};
Month::Month( int monthNumber){
monthNum = monthNumber;
};
Month::Month(){
};
void Month::getMonthByNumber(istream& in){
in >> monthNum;
};
void Month::getMonthByName(istream& in){
char c1, c2, c3;
in >> c1 >> c2 >> c3;
c1 = tolower(c1);
c2 = tolower(c2);
c3 = tolower(c3);
if (c1 == 'j' && c2 == 'a' && c3 == 'n'){
monthNum = 1;
} else if (c1 == 'f' && c2 == 'e' && c3 == 'b'){
monthNum = 2;
} else if (c1 == 'm' && c2 == 'a' && c3 == 'r'){
monthNum = 3;
} else if (c1 == 'a' && c2 == 'p' && c3 == 'r'){
monthNum = 4;
} else if (c1 == 'm' && c2 == 'a' && c3 == 'y'){
monthNum = 5;
} else if (c1 == 'j' && c2 == 'u' && c3 == 'n'){
monthNum = 6;
} else if (c1 == 'j' && c2 == 'u' && c3 == 'l'){
monthNum = 7;
} else if (c1 == 'a' && c2 == 'u' && c3 == 'g'){
monthNum = 8;
} else if (c1 == 's' && c2 == 'e' && c3 == 'p'){
monthNum = 9;
} else if (c1 == 'o' && c2 == 'c' && c3 == 't'){
monthNum = 10;
} else if (c1 == 'n' && c2 == 'o' && c3 == 'v'){
monthNum = 11;
} else if (c1 == 'd' && c2 == 'e' && c3 == 'c'){
monthNum = 12;
} else {
cout << "Sorry that is not a month";
exit(1);
}
};
void Month::outputMonthNumber(ostream& out){
out << monthNum;
};
void Month::outputMonthName(ostream& out){
switch (monthNum)
{
case 1:
out << "Jan";
break;
case 2:
out << "Feb";
break;
case 3:
out << "Mar";
break;
case 4:
out << "Apr";
break;
case 5:
out << "May";
break;
case 6:
out << "Jun";
break;
case 7:
out << "Jul";
break;
case 8:
out << "Aug";
break;
case 9:
out << "Sep";
break;
case 10:
out << "Oct";
break;
case 11:
out << "Nov";
break;
case 12:
out << "Dec";
break;
default:
exit(1);
}
};
Month Month::nextMonth(){
int nextMonth = monthNum +1;
if(nextMonth == 13){
nextMonth = 1;
}
return Month(nextMonth);
};
int Month::monthNumber(){
return monthNum;
};