Hello guys, I've trying to figure this problem out, but I can't wrap my head around it. I have to 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.(Use the cast operator, static_cast<char>(), for numbers >= 10)
Also your program should let the user know if they had entered an invalid number as well, a number that is not between 0 and 35 is considered invalid and you should output something like: You entered an invalid value, run your program again.
#include <iostream>
usingnamespace std;
int main()
{
int num;
int A = 10;
int B = 11;
int C = 12;
cout << "Enter and number between 0 and 35.\n";
cin >> num;
if (num <= 9)
cout << num << endl;
elseif (num >= 10);
cout << static_cast<char>(num) << endl;
return 0;
}
To convert the number to a letter, subtract 10, then add 'A'. So line 18 should be cout << static_cast<char>(num-10+'A') << endl;
Don't forget to check for invalid input. Do this before line 15
a char 'A' is basically the same thing as the number 65, according to ASCII values.
So, if number is 10, you want to print out the ascii value of A, which is 65.
You can add chars just like you can add ints using the + operator.
For example, doing 10 + 'A' is the same thing as doing 10 + 65, which, when static_casted to char, will print 'K', the ASCII value for 75.
You want a simple equation that follows this chart