Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当我键入一个字符时,getch返回两个字符_C++_Visual Studio 2017_Console Application - Fatal编程技术网

C++ 当我键入一个字符时,getch返回两个字符

C++ 当我键入一个字符时,getch返回两个字符,c++,visual-studio-2017,console-application,C++,Visual Studio 2017,Console Application,当我使用getch时,它总是在读取的字符后面附加一个空字符 当我使用以下代码时: #include "stdafx.h" #include <conio.h> int main() { char c = 0; while (c != 'x') { c = _getch(); printf("Char read: <%c> \n", c); } } #包括“stdafx.h” #包括 int main()

当我使用getch时,它总是在读取的字符后面附加一个空字符

当我使用以下代码时:

#include "stdafx.h"
#include <conio.h>

int main()
{
    char c = 0;

    while (c != 'x')
    {
        c = _getch();
        printf("Char read: <%c> \n", c);
    }
}
#包括“stdafx.h”
#包括
int main()
{
字符c=0;
而(c!=“x”)
{
c=_getch();
printf(“字符读取:\n”,c);
}
}
当我按下“asdx”键时,它在控制台上返回以下内容:

字符读取:
字符读取:<>
字符读取:
字符读取:<>
字符读取:
字符读取:<>
字符读取:
这是在VS2017中编译的一个全新的单文件项目,运行在windows 10控制台窗口上。我尝试删除_UNICODE和UNICODE定义

好吧,废话

这是windows中的一个(相当新的)错误

“使用_getch()函数构建控制台应用程序时 每次按键都会突然返回两次“

从microsoft获得以下响应:

“感谢您的报告!这将在未来的windows上修复。” 更新。”

更新: 如上面的链接所述,将运行时设置为静态链接与以前版本的C运行时将解决此问题,但您需要确保所有相关项目(例如,如果您正在构建库)也使用相同的运行时。(我测试了它)

如果您不想(或者不能)使用以前版本的通用C运行时,可以使用
\u getwch
而不是
\u getch
,因为问题不会影响
\u getwch
函数


请注意,
\u getwch
可以返回Unicode字符,因此对于某些键,它可能返回不同于
\u getch
的值。例如,对于
字符,
\u getch
返回0x3f(
),而
\u getwch
返回0x20ac(Unicode值表示€)

如何知道它是空字符?顺便说一句,getch()返回
int
。对我来说工作正常,您是否尝试打印c的数值以查看它是什么?我知道它是null,因为我在调试器中跟踪了它。我尝试使用int或char,得到了相同的结果。这显然是某些版本的Windows SDK中存在的问题,请参阅@AlanBirtles:它在调试模式下对我有效,但在发布模式下失败。与OP相同的行为。
Char read: <a>
Char read: < >
Char read: <s>
Char read: < >
Char read: <d>
Char read: < >
Char read: <x>