Problem: Write a program that prompts the user to input an integer between 0 and 35. If the number is less than or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12,..... and Z for 35. (Here's the part I don't understand) Use cast operator, static_cast<char>(), for numbers >= 10. Here's my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x;
cout << "Enter a number between 0 and 35:";
cin >> x;
cout << endl;
switch(x)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
cout << x << endl;
case 10:
cout << "A" << endl;
break;
case 11:
cout << "B" << endl;
break;
case 12:
cout << "C" << endl;
break;
case 13:
cout << "D" << endl;
break;
case 14:
cout << "E" << endl;
break;
case 15:
cout << "F" << endl;
break;
case 16:
cout << "G" << endl;
break;
case 17:
cout << "H" << endl;
break;
case 18:
cout << "I" << endl;
break;
case 19:
cout << "J" << endl;
break;
case 20:
cout << "K" << endl;
break;
case 21:
cout << "L" << endl;
break;
case 22:
cout << "M" << endl;
break;
case 23:
cout << "N" << endl;
break;
case 24:
cout << "O" << endl;
break;
case 25:
cout << "P" << endl;
break;
case 26:
cout << "Q" << endl;
break;
case 27:
cout << "R" << endl;
break;
case 28:
cout << "S" << endl;
break;
case 29:
cout << "T" << endl;
break;
case 30:
cout << "U" << endl;
break;
case 31:
cout << "V" << endl;
break;
case 32:
cout << "W" << endl;
break;
case 33:
cout << "X" << endl;
break;
case 34:
cout << "Y" << endl;
break;
case 35:
cout << "Z" << endl;
break;
default:
cout << "Invalid entry." << endl;
break;
}
system("PAUSE");
return 0;
}
How can I integrate static_cast<char> within my code to make it simpler and shorter?
For the numbers higher than 9 you can use static_cast<char> ( x - 10 + 'A' )x - 10 so that you will start from 0 + 'A' so you add the 'A' ASCII value to that