简体   繁体   中英

c++ Using _getch() for instant user input, but it keeps returning different numbers

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.

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

_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM