Switch structure

Am new to Cplusplus and I am learning about the switch structure. I coded a program which compares the fine that a library takes in response to the number of days that it takes the student to bring the book. Originally the fine for 1 to 5 days is $5
6 to 11 days is $10 and 11 to 30 days is $50 which after the 30th day the membership will be cancelled. I wanted to shorten it but don't actually know how to go about it so I had to make a switch with 31 cases which I know is probably time consuming and cumbersome. The program runs alright but I will be grateful if I can get any assistance on how to shorten it, Thank you.

Please this is the code

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
 // Learning Switch structure
#include <iostream>
using namespace std;
int main() {
	int date= 11;
	
	switch (date) {
		case 1:
		cout << "Your fine is 5 dollars: ";
		break;
		case 2:
		cout << "Your fine is 5 dollars: ";
		break;
		case 3:
		cout << "Your fine is 5 dollars: ";
		break;
		case 4:
		cout << "Your fine is 5 dollars: ";
		break;
		case 5:
		cout << "Your fine is 5 dollars: ";
		break;
		case 6: 
		cout << "Your fine is 10 dollars: ";
		break;
		case 7: 
		cout << "Your fine is 10 dollars: ";
		break;
		case 8: 
		cout << "Your fine is 10 dollars: ";
		break;
		case 9:
		cout << "Your fine is 10 dollars: ";
		break;
		case 10: 
		cout << "Your fine is 10 dollars: ";
		break;
		case 11: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 12: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 13: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 14: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 15: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 16: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 17: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 18: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 19: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 20: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 21: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 22: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 23: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 24: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 25: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 26: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 27: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 28: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 29: 
		cout << "Your fine is 50 cedis: ";
		break;
		case 30: 
		cout << "Your fine is 50 dollars: ";
		break;
		case 31:
		cout << "Your subscription is cancelled: ";
		
		return 0;
	}
}
Last edited on
> The program runs alright
11 days doesn't.

You can do this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		cout << "Your fine is 5 dollars: ";
		break;
		case 6: 
		case 7: 
		case 8: 
		case 9:
		case 10: 
		cout << "Your fine is 10 dollars: ";
		break;

yea switches have a fall through feature.

so you can say
case 1:
case 2:
case 3:
case 4:
case 5: cout << "Your fine is 10 dollars: "; //this happens for 1,2,3,4 and 5.
break; // stop the fall-thru chain here, and restart a new one for 6 to 10

case 6:
...


return 0 should not be in the switch at all, and its optional for end of main these days it is usually left off.

its probably slightly better to say
default: cout << "Your subscription is cancelled: "; //for 31 and greater values.
even if 31 is 'the biggest day of the month' it still works if the user put in 100. or if you want to catch > 31 as errors, keep the special 31 case and make a default "error in date" for 32... all others. instead of case you just say default: code;

some things are better as switches because of the fall through (consider if you wanted to do the same thing for 1-5 but something special for 1, then you can fall thru the 1 to do the same thing for 1-5 while doing the special case in the 1, exploiting the power of the fall thru without spaghetti logical conditions), and because switches are usually implemented very efficiently in compiled code for performance. But some things are better written with if/else statements. While you can convert most data to unique integers for the switch, this has a cost, so using them on non integer data is a decision point to consider, and making sense of the logic is a decision point (fall thrus can be hard to follow compared to boolean conditions).
this could be written super compactly in multiple ways without a switch at all, but given your study on switches, lets leave it there for now.

consider...
case 1: cout << "you will now be fined for your annoying lateness" << endl;
/// note lack of a break here.
case 2:
case 3:
case 4:
case 5: cout << "Your fine is 5 dollars: ";

so 1-5 here all print "Your fine is 5 dollars: " but for 1 it ALSO prints the other string first.
Last edited on
Thank You Mr Jonnin and thank you Mr salem,
It works perfectly well now.
Last edited on
Some compilers report a warning on fall-through. Since c++17 the attribute [[fallthrough]] can be used to suppress these warnings. https://en.cppreference.com/w/cpp/language/attributes/fallthrough
Topic archived. No new replies allowed.