_getch() -- seemingly irrational fears

closed account (230MDjzh)
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:
1
2
3
4
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
IMO. At the end of the day, it doesn't matter squat what function your going to use. _getch() is not part of the C++ standard, so there is no guarantee your compiler is going to have it. http://en.wikipedia.org/wiki/Conio.h


1. Valid, but the only use I've seen of local applications from the cmdline using passwords is from newbie programs who are doing assignments.

2. getLine() returns a string, which can be used in conditional statements too.

3. Not really a big deal.

4. _getch() and system("pause") both pause. So it doesn't matter how long they take in CPU cycles. The wasted cycles while it waits for your feedback is going to be infinitely more.

5. system("pause") only locks current thread too.

As stated, not part of the C++ standard. Not implemented the same way in compilers where conio.h is present.
Last edited on
1. Yes I agree with Zaita I've had to use it for starting off assignments.

2. Yes getLine() Does return a string.

3. Not having to press return was helpful when i first started with c++. But now days I just use isKeyDown() and isKeyUp - depending on what library I'm using.

4. Correct they both pause.

5. <3 Multi-threading - but yes it does only lock the current thread.

Don't get me wrong when I have a quick task to write for myself I do like the conio.h but it really isn't needed at all. And well as Zaita said again - not part of the C++ Standard.
http://en.wikipedia.org/wiki/Hubris

Don't encourage bad habits (or bad ways of thinking about programming).

[edit]
Hmm, I realize my response may seem rather harsh.
If you only wish to start a discussion to learn more, then I'm all for it.
I'm up for some learning.
Last edited on
Hey like I said I don't mind using it for certain things like if I have to make a quick program purely for learning - Then I get good use out of it :)
Agreed, but the OP's stated purpose is to convince others that it "deserves to be used much more often".

When people are taught and accept such functions without knowing or understanding the ramifications of their use then code quality suffers and you become a liability to your employer/company and other programmers who have to put up with your cavalier way of looking at the world.

The way people use system("PAUSE") and getch() variants today shows that they have no concept or appreciation for the way the world was before standard streams existed, and the revolution it was.

But, those who ignore history are destined to be trampled by it.
Topic archived. No new replies allowed.