20

Somewhere back in time I did some C and C++ in college, but I didn't get to many attention to C++. Now I wish to pay some attention to C++ but when I'm using the getch() function, I get the warning from below.

Warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.

Now, I'm using VS 2005 express, and I don't know what to do about this warning. I need to use getch() after I printf() an error message or something else which require a key hit.

Can you help me with that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Duncan Benoit
  • 3,297
  • 8
  • 29
  • 26
  • 4
    Did you read the error message? – jalf May 02 '09 at 15:09
  • 2
    @jalf: The "error message" is wrong/misleading, so I don't see how reading it would help... – R.. GitHub STOP HELPING ICE Sep 14 '11 at 22:32
  • 12
    @R..: No it isn't. It very clearly states the compiler's reason for issuing the warning, and it states just as clearly what they want you to do to *avoid* the warning. You and I may not agree with Microsoft's unilateral decision to "deprecate" standard functions, but the warning very clearly explains the issue, whether or not you agree with the underlying reasoning. If you want the warning to go away, and the warning actually says "please use this function instead", then reading the warning certainly helps. – jalf Sep 14 '11 at 22:37
  • 5
    Mentioning POSIX is what's wrong and misleading; this has nothing to do with POSIX. Moreover the function is not a standard function; it's a function name used by conio (and I believe also curses) for interactive single-character reads from the keyboard with no buffering. – R.. GitHub STOP HELPING ICE Sep 14 '11 at 22:39
  • Related post : [Alternative function in iostream.h for getch() of conio.h?](https://stackoverflow.com/q/1377403/465053) – RBT Jun 15 '18 at 09:18

6 Answers6

26

Microsoft decided to mark the name without underscore deprecated, because those names are reserved for the programmer to choose. Implementation specific extensions should use names starting with an underscore in the global namespace if they want to adhere to the C or C++ Standard - or they should mark themselves as a combined Standard compliant environment, such as POSIX/ANSI/ISO C, where such a function then corresponds to one of those Standards.

Read this answer about getcwd() too, for an explanation by P. J. Plauger, who knows stuff very well, of course.

If you are only interested to wait for some keys typed by the user, there really is no reason not to use getchar. But sometimes it's just more practical and convenient to the user to use _getch and friends. However, those are not specified by the C or C++ Standard and will thus limit the portability of your program. Keep that in mind.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 1
    Actually there is a difference between getchar and getch. getch blocks until some key is pressed. getchar blocks until enter is pressed. – Patrick Sturm Dec 06 '17 at 11:43
9

If you're into C++, why printf and getch? Consider using cout and cin.get instead.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
6

use _getch() instead of getch()

shahzaib
  • 69
  • 1
  • 1
  • 6
    Could you explain why the asker should use one instead of the other? Fixing the problem is good but understanding the problem and why the solution works is even better. – Michael Celey Apr 02 '13 at 19:26
5

As the error says, the name getch() is deprecated (as that was a Microsoft-specific extension), and _getch() is recommended instead. I'm not sure where POSIX comes into it; POSIX does not define getch(), but instead includes getchar() as specified by the ISO C standard (page 298, draft linked to because final standard is not free). The solution would be to do as suggested and use _getch(), or use the standard C getchar().

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • 3
    `getch` has little to do with `getchar`. Rather it's a conio (and I believe also curses) interface whose purpose is to interactively read a single character from the keyboard without buffering/delay. – R.. GitHub STOP HELPING ICE Sep 14 '11 at 22:41
  • POSIX doesn't come into it in the case of this particular function, but *most* of the functions that were deprecated in favour of underscore versions were POSIX-like functions such as `open` or `mkdir`. Guess they just didn't want to bother creating a separate warning message for the few exceptions. – Harry Johnston Oct 27 '15 at 03:17
1

Why do you need this? Why not use getchar() if you need to capture a single character?

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 12
    getchar is line buffered, thus it will wait for a newline character. _getch() won't, but return immediately. I can understand him sometimes it's really useful ("press x to zoom", for example). – Johannes Schaub - litb May 02 '09 at 15:04
  • 1
    I don't think it has anything to do with buffering. getchar() reads in a character at a time. There is then fgets and the entire range of Xscanf() functions (albeit difficult to use). But as I said, it is difficult to suggest something without proper context, particularly a non-standard extension. – dirkgently May 02 '09 at 15:10
0

you can use "std::getc(stdin);". It works like 'getch();' in my programs. I think It's a good alternative for 'getch()' If I'm wrong, tell me.

#include <cstdio>

switch (std::getc(stdin)) {
    case 'a':   cout << 'a' << endl ; break;
    case 'b':   cout << 'b' << endl ; break;
    default:    cout << '$' << endl; break;
}

It outputs 'a' if you input 'a' on console.

aariow
  • 76
  • 1
  • 9