-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonths_switch_statement.cpp
74 lines (62 loc) · 1.6 KB
/
months_switch_statement.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
#include <iostream> // std::xcin, std::cout,
// Here `unsigned char` is used to store a number and not a letter.
// A `char*` (character pointer) is a string, since a string is just an array of `char`s
// The character pointer points to the first element and you can increment/decrement it
// to see the rest of the elements.
// If this confuses you, you can replace `char*` with `std::string`
// which isn't optimized, but is often easier to grasp.
char* numberToName(unsigned char monthNum){
/// returns a string which is the name of the month corresponding
/// to the given number.
switch (monthNum) {
case 1://code between the case and "break;" is run if monthNum = 1
return "January";
break;
case 2:
return "Febuary"; // in this example, calling `break` is redundant since execution exits
break; // after the function `return`s a string.
case 3:
return "March";
break;
case 4:
return "April";
break;
case 5:
return "May";
break;
case 6:
return "June";
break;
case 7:
return "July";
break;
case 8:
return "August";
break;
case 9:
return "September";
break;
case 10:
return "October";
break;
case 11:
return "November";
break;
case 12:
return "December";
break;
default:
return "invalid";
break;
}
//this will never get returned.
return "an error :P";
}
int main(){
int mn; // declare month number
std::cout <<"Month Number: "; // prompt
std::cin >>mn; // get input
std::cout <<"The month is " <<numberToName(mn) <<"\n\n"; // print it out (call function)
// infinite loop...
return main(); // keep playing
}