简体   繁体   English

c ++使用_getch()进行即时用户输入,但它不断返回不同的数字

[英]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. 我希望choiceOne能够=用户输入的内容,但是会输出(48+用户输入),所以如果我输入0,它将输出48,如果我输入5,它将输出53。我不确定48会在哪里从。

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. 您正在读取字符变量,但将它们存储在int类型的变量中。 this will convert the input from char to int. 这会将输入从char转换为int。 what you are seeing is the corresponding ASCII integer values for the characters you are inputting. 您所看到的是所输入字符的相应ASCII整数值。 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 将choiceOne更改为char类型的变量,以便您的代码起作用,并在线搜索ASCII表以获取每个标准字符的所有ASCII代码的完整参考

_getch returns ASCII coding. _getch返回ASCII编码。 If you press 0, it gets ASCII coding of the character '0', which in hex is 0x30, and in dec is 48. 如果按0,它将获得字符“ 0”的ASCII编码,十六进制为0x30,十进制为48。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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