Can you condense this switch statement?

Upon completing this assignment I get this extremely long switch statement. It gets the job done, but I am wondering if there is any way to condense in order to get the same results. Thank you. ***Must use a switch statement***


QUESTION:
Write a program that asks the user to enter a number within the range of numbers equivalent to the lowercase alphabet. Use a switch statement to display the alphabet version of that number.

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
  #include <iostream>
using namespace std;

int main() {
	int num;
	
	cout << "Enter a number between 97 - 122 (included) to find it's corresponding ASCII character. " << endl;
	cin >> num;
	
	switch (num) {
		case 97:
			cout << num << " is the number for " << char(num) << ".\n";
			break;
			
		case 98:
			cout << num << " is the number for " << char(num) << ".\n";
			break;
			
		case 99:
			cout << num << " is the number for " << char(num) << ".\n";
			break;
			
		case 100:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 101:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 102:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 103:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 104:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 105:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 106:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 107:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 108:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 109:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 110:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 111:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 112:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 113:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 114:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 115:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 116:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 117:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 118:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 119:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 120:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 121:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		case 122:
			cout << num << " is the number for " << char(num) << ".\n";
			break;

		default:
			cout << "Not a number or not in range of lowercase alphabet.\n";
			break;
	}	
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
using namespace std;

int main() {
	int num;
	
	cout << "Enter a number between 97 - 122 (included) to find it's corresponding ASCII character. " << endl;
	cin >> num;
	
	//check for out of range here, in a separate if-statement
	
	switch (num%2) {
                case 1: cout << num << " is the number for " << char(num) << ".\n";
			break;
		case 0: cout << num << " is the number for " << char(num) << ".\n";
			break;
	}

	return 0;
}


Only downside is that you can't really have a default case, but there's an easy work around for this as you can simply check out-of-range input before the switch.
Last edited on
Look at the example at the bottom of: http://www.cplusplus.com/doc/tutorial/control/

There is an another way:
1
2
3
int inrange = ( 97 <= num && num <= 122 ) ? 1 : 0;
switch ( inrange ) {
...

That, however, is an obfuscated way to write:
1
2
3
4
5
if ( 97 <= num && num <= 122 ) {
  // show
} else {
  // not in range
}

Since you have to use switch, the method in tutorial is what the teacher probably expects.
In fact, you can make it even more condense:

1
2
3
4
	switch (0/num) {
		case 0: cout << num << " is the number for " << char(num) << ".\n";
			break;
	}
In this case, it is easy to condense the switch statement as all you need to do is find a switch-condition which leads to a single case. The following conditions would all lead to a single case:

switch(num / num)

switch(num % num)

switch(num - num)

switch(num * 0)
Last edited on
Thank you all for the input, I ended up choosing @Arslan7041's idea for the argument using (num * 0) so the only case is 0. This cut the length down of my program to well below half what it was.
Topic archived. No new replies allowed.