-1
int choiceOne = 0;
choiceOne = _getch();
cout << choiceOne;
_getch();
system("CLS");

I would like choiceOne to = what the user enters, but it outputs (48+ The user input) So if I enter 0 it will output 48, if I enter 5 it will output 53. I'm not sure where the 48 is coming from.

If more code is necessary I can post it.

Max Mcgregor
  • 101
  • 9
  • 1
    `0 != '0'` https://en.wikipedia.org/wiki/ASCII – user657267 Oct 21 '15 at 00:00
  • Possible duplicate of [Why does subtracting '0' in C result in the number that the char is representing?](http://stackoverflow.com/questions/15598698/why-does-subtracting-0-in-c-result-in-the-number-that-the-char-is-representing) – user657267 Oct 21 '15 at 00:01
  • The difference between numbers and representations of numbers is one of the most important things a programmer must understand. You are entering the *digit* "5" and it is outputting the number fifty-three. Since numbers and digits are totally different things, this should not be surprising. – David Schwartz Oct 21 '15 at 00:09

2 Answers2

2

you are reading in character variables but storing them in an a variable of type int. this will convert the input from char to int. what you are seeing is the corresponding ASCII integer values for the characters you are inputting. alter choiceOne to be a variable of type char for you code to work, and search for the ASCII table online to get a full reference of all the ASCII codes for each standard character

David Loughnane
  • 143
  • 1
  • 3
  • 11
0

_getch returns ASCII coding. If you press 0, it gets ASCII coding of the character '0', which in hex is 0x30, and in dec is 48.

Yorkwar
  • 1,204
  • 1
  • 11
  • 27