So,
I've been coding C++ for a while now, and in that time I've of course surfed the web a fair bit. Is it just me, or does everybody on the internet just hate conio.h's _getch()?

I've seen a lot of forums where people are asking the best way to pause a program, and a lot of people absolutely refuse to use _getch(), or getch(). So, for that reason, I've come up with a list of reasons why I think _getch is an extremely useful function, and deserves to be used much more often.

1. _getch() does not echo the text as it is entered in like other popular functions (such as std::cin.get()). This, despite public opinion is actually an extremely important feature, in that it allows the program to substitute in any other output it wants. How else would one, for example, create a function that allows the user to input a password, where instead of their password appearing on screen, asterisks (*) do?

2. _getch() returns a char, so it can therefore be embedded right into conditions, like this:

if(_getch()==' ')
 {
  //Code
 }

3._getch() does not require [return] to be pressed at the end. Again, another extremely useful function. If the program comes to a point where there is a lot of text on the screen, an output such as "Press Any Key to Continue" may be necessary; to me it just looks silly to have whatever garbage they input collect on the screen until the user presses [return] - especially when this could be avoided by simply using _getch().

4._getch() is a very quick and low rent function (this in particular is a shot at the somewhat popular system("pause")). _getch() doesn't require abundant amounts of CPU to execute, nor does it require interaction with the OS which makes it far more portable. Also, unlike system("pause"), _getch() is adaptable, meaning you can output any message you choose, unlike system("pause").

5. _getch() only locks the thread from which it is called. Now to beginner programmers, that doesn't really mean anything, but to multi-threaded programmers, it represents a world of possibilities wherein one thread can sit with the express purpose of monitoring key strokes, possibly waiting for a specific key to be pressed.

------

So, as far as I'm concerned _getch() is the only way to go when you want to pause a program, or when you want single-key non-echoed input.

Regardless, there seems to be a lot of haters out there on the internet, so if you guys can see any legitimate reason not to use _getch() please tell me.

Thanks

Recommended Answers

All 2 Replies

>so if you guys can see any legitimate reason not to use _getch() please tell me.
I use a compiler that doesn't support conio.h, now what do you suggest, Mr. Smarty Pants? The reason so many people "hate" getch is because it's not a standard function. When you write code that uses it, you can only guarantee that the code will compile on your compiler. Is that bad? It really depends on what the situation calls for, as any decent programmer will tell you.

Perhaps you should try to understand the "haters" instead of resorting to name calling and coming up with reasons why you think you're better than they are. Taking an extreme stance (either "getch is EBIL!" or "getch is da BOMB!") suggests that you lack experience and/or good judgment.

I don't hate the functions in conio.h, I just whish they had become part of the C standards so that they could be used more frequently. kbhit() and getch() are very very useful functions, but perhaps they never became part of the standards is because they would be difficult at best to implement on some operating systems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.