static_cast<char>

Write your question here.

[code]
Put the code you need help with here.
if (num==17)
cout << "Number output is: 'H' "<< endl;
if (num==18)
cout << "Number output is: 'I' "<< endl;
if (num==19)
cout << "Number output is: 'J' "<< endl;
if (num==20)
cout << "Number output is: 'H' "<< endl;

else if (num >= 20 && num <= 35) // else if the number is greater than or equal to 20 and number is less than or equal to 35//
cout << static_cast<char>('H' + (num - 10)) << endl;

___________________________________________________________
cout << static_cast<char>('H' + (num - 10)) << endl;

How does this static_cast operate? 'H' + num-10 ? Is this taking 10 from the number entered?
could someone help me with this question? I know static_cast rounds the number, but letters? How does the program know to output the next char once the number is input?
What happens is that it takes the numerical value of 'H', implicitly casts it to int, adds num - 10, and then takes that new value and re-casts it into a character for displaying.
static_cast is used for converting one type into another. 'H' is char but when you have char + int the result will be an int. You don't want to print the value as an integer (ASCII value) so you convert it into a char to make it print the value as a character.
Thanks you guys are a life saver :)
Topic archived. No new replies allowed.