getch() 在最新版本的 Microsoft Visual Studio 2017 C 中无法正常工作

Ant*_*sić 4 c getch visual-studio-2017

我花了过去几周的时间开发一个基本上依赖于 getch() 函数的应用程序,然后我最近决定更新 Visual Studio,却发现我的程序完全损坏并且不再工作。

这是我所拥有的一个简单示例:

main(){
while (1){
 //The program loops until 's' is pressed (this still works)
 com=getch();
  if(com=='s'){
   //The program used to stop here and wait for input (now it doesn't)
   com=getch();
   if(com=='d') printf("Victory!\n");
   else break;
  }
}
} 
Run Code Online (Sandbox Code Playgroud)

*这不是我程序的一部分,这只是一个示例,需要按“s”然后按“d”才能获胜

现在,这在我更新之前就起作用了,我知道,因为我花了 50 多个小时来开发该程序,它的工作方式如下:

程序将到达 getch() 并等待我的输入,如果我按 's',if 会触发执行其功能,然后它将到达第二个 getch() 并等待我的输入,因此您需要按 'd '并获胜!

要点是,它曾经在每次 getch() 时等待我的输入!

但现在,新的更新它等待第一个 getch() 但 complitley 忽略了第二个,这很好地结束了程序并且没有办法获胜。

也许我做了什么,也许 getch() 现在是非法的,我不知道,我晚餐不高兴,我不知道该怎么办......

无论如何,提前致谢,如果您还有什么需要了解的,请随时询问。我是编程新手,所以不要期望任何高水平的答案!

编辑:我花了几个小时来探索这里的代码:

#include <conio.h>
#include <stdio.h>
main(){
    char com;
    while(1){
        com=getch();
        printf("You pressed: %c\n",com);
    }
}

 Here are the results:
 You pressed: d 
 You pressed: 
 You pressed: s 
 You pressed: 
 You pressed: a 
 You pressed:
Run Code Online (Sandbox Code Playgroud)

输入为“d”、“s”和“a”。

M.M*_*M.M 5

这是 Windows 中的一个错误。根据此线程,系统 DLLucrtbase.dll版本 17134 引入了该错误。此 DLL 由 VS2017 和 Windows 10 build 1803 分发。

他们承诺修复它,但目前还没有修复。此错误破坏了许多使用_getch().


要解决该问题,您可以:

  • 修改您的代码以丢弃额外的返回(它们具有 value 0)。
  • _getwch()代替使用。
  • 请改用 Windows APIReadConsole函数(有关示例代码,请参阅链接线程)。