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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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.
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:
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.