Antonio Klasić
Antonio Klasić

Reputation: 66

getch() doesn't work properly in the latest version of Microsoft Visual Studio 2017 C

I spent the last couple of weeks working on an aplication which basicly relies on the getch() function, then I recently decided to update visual studio only to find out that my program is completly broken and doesn't work anymore.

Heres a simple example of what I have:

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;
  }
}
} 

*This is not a part of my program this is simply an example which requires to press 's' then 'd' in order to win

Now, this worked before I updated, I know because I spent over 50 hours working on the program and it worked the following way:

The program would reach getch() and wait for my input, if I pressed 's', the if would trigger doing its fuction and then it would reach the second getch() and wait for my input, so you'd press 'd' and win!

Main point is, it used to wait for my input at every single getch()!

But now, new update it waits for the first getch() but complitley ignores the second one which, well ends the program and there is no way to win.

Maybe I did something, maybe getch() is now illegal, I don't know, im supper unhappy, I don't know what to do...

Anyway thanks in advance, if there is anything more you need to know, feel free to ask. I'm new to programing so don't expect any high level answers!

Edit: I spent a few more hours of exploring heres the code:

#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:

The inputs were 'd', 's', and 'a'.

Upvotes: 4

Views: 3033

Answers (1)

M.M
M.M

Reputation: 141618

This is a bug in Windows. According to this thread, the system DLL ucrtbase.dll version 17134 introducted the bug. This DLL is distributed by both VS2017 , and Windows 10 build 1803.

They have promised to fix it but no fix exists yet. This bug has broken the behaviour of many compiled applications that use _getch().


To work around the problem, you could:

  • modify your code to discard the extra returns (they have value 0).
  • Use _getwch() instead.
  • Use the Windows API ReadConsole function instead (see linked thread for sample code).

Upvotes: 5

Related Questions