tags:

views:

1181

answers:

4

Hi there,

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 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 se getch() after I printf() an error message or something else which require a key hit

Can you help me with that?

+2  A: 

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

dirkgently
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
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
+10  A: 

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.

Johannes Schaub - litb
+1  A: 

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

Eli Bendersky
A: 

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