-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-switch_statements.cpp
56 lines (41 loc) · 1.33 KB
/
16-switch_statements.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
#include <iostream>
using namespace std;
string getDayOfWeek(int dayNum);
int main()
{
cout << "Enter a day of the week in numbers (0-6)" << endl;
int dayNum;
cin >> dayNum;
cout << getDayOfWeek(dayNum) << endl;
return 0;
}
string getDayOfWeek(int dayNum)
{
string dayName;
switch(dayNum){ // Could use several (else) if statements
case 0:
dayName = "Sunday";
break; // Needed to exit switch statement when case is true
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
default: // Gets executed if none of the cases are true
dayName = "Invalid dayNum";
}
return dayName;
}