Need Help for Homework

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace 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;
	else if (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
First thing is you have a semi-colon on line 17 you don't want.

Now the real thing:

Did your instructor explain what ASCII values are?
http://www.asciitable.com/

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
1
2
3
4
5
input  | output
10     | 65 (which is 'A')
11     | 66 (which is 'B')
12     | 67 ('C'... etc)
...     |  ...

output = input + 55
Last edited on
Topic archived. No new replies allowed.