1,105,534 Community Members

replacement of getch()

Member Avatar
lara_
Newbie Poster
17 posts since Jul 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i just want to whether there is a replacement for getch() in C++.

my friend said getch() is C function.

thanks!

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

>i just want to whether there is a replacement for getch() in C++.
There isn't a standard equivalent for getch in either C or C++.

>my friend said getch() is C function.
No, getch is a compiler extension. You can use it in either C or C++ if your compiler supports it, but for the most part you don't need to.

For what purpose were you intending to use getch? The most common by far is keeping a console window from closing when you run the program from a Windows IDE:

#include <conio.h>

int main()
{
  // Your program here

  getch();
}

That's not a recommended solution, and most experienced programmers will recommend that you use cin.get:

#include <iostream>

int main()
{
  // Your program here

  std::cin.get();
}
Member Avatar
Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 25 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Okay, I have to say that i often use getch() not just to halt the console window.
I used getch() along with kbhit() [to get the key if pressed any]--very important use of getch().
And there are times when I have given the user to choose from a number of options (menu items) and used getch() to get the choice. I know the latter can be done with any input functions but getch() makes it simpler-- scanf(), cin and cin.get() will show the keys as u type in and then u have to press enter, whereas in getch() u just press the key and it's not even shown. getch() also makes things easier as u can enter only one key and there's no chance of receiving any degenerate inputs. On the otherhand it's very common for scanf(), cin and cin.get() to mess up with the inputs when only one keystroke is sufficient and desireable.

i would really love to know if there's any other way to get the functionality of getch()(i.e without using getch()) in the above mentioned cases.

Member Avatar
lara_
Newbie Poster
17 posts since Jul 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i'm using getch() at password entering that display * when password is entered.

Member Avatar
Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 25 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

i'm using getch() at password entering that display * when password is entered.

yes, i have used it for the same purpose for one of my programs too. I hope some one here will suggest how to work that out without using getch(), since according to "Narue" its a compiler extension and not a C function. Now we dont to code using something that is not part of the language.

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
1
 

i would really love to know if there's any other way to get the functionality of getch()(i.e without using getch()) in the above mentioned cases.

The problem with unbuffered input is that it becomes the responsibility of the programmer to handle the buffering. Do you allow the user to make a typing mistake? Then handle the backspace. Then you can't have immediate response because you need an "enter" for a 'final' answer. Then you're back where you started with the standard way.

The reason it may 'work well' might be because other issues have been overlooked. It seems a more reasonable approach is to use standard functions that have already taken such issues into account and move forward. Yet this opinion is revisited thousands of times every year by programmers. Their first goal seems to be clearing the screen. Frankly, if the command shell commands behaved this way, I'd be pretty annoyed: execute two 'dir' commands consecutively, and the second throws away the result of the first -- annoying.

Ah well, it seems new programmers always must go full circle. I do try to get them to go the right way first, otherwise it seems to be a much longer journey.

That said, any nonstandard function has the implied disclaimer, "use at your own risk"! :p

Member Avatar
djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Try
#include <iostream.h>
void main()
{ ...... /* your program here */
cin.get();
}

It should work .

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

>It should work
No, it should do whatever the hell it wants because you've invoked undefined behavior. And even when the undefined behavior is something intelligent, iostream.h is a nonstandard header, so you can't be sure that cin.get() even exists, much less that it does what you want it to.

Member Avatar
Eagle-Man
Newbie Poster
3 posts since Sep 2007
Reputation Points: 16 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

To anyone reading this thread:
getch() is still available in the conio.h header file (at least in MinGW). cin.get() acts the same as getchar() - it echos the pressed key and waits for enter to be pressed.

And don't worry, Narue is wrong - iostream is very standard for C++.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
1
 

>To anyone reading this thread:
You realize the thread is over three years old, yes?

>getch() is still available in the conio.h header file (at least in MinGW).
I'm sure it works just fine on your compiler, but because it's non-standard, that means it's not guaranteed to work on my compiler, or someone else's compiler. Is this that difficult of a concept for you people to grasp?

>cin.get() acts the same as getchar() - it echos the
>pressed key and waits for enter to be pressed.
Assuming that's what getchar does. getchar makes a call to a system function that performs the actual read, and that system function (whatever it may be) isn't required by the C++ standard to provide cooked input. However, I'll give this to you because I can't think of any systems off the top of my head that don't buffer up to a line feed. It's a safe assumption.

>And don't worry, Narue is wrong
Narue is always right[1]. You'd do well to realize that before truly embarrassing yourself.

>iostream is very standard for C++.
<iostream> is standard, <iostream.h> is not. Once again, this is a very simple concept, but I'll assume that you're just having trouble with reading for comprehension and missed the ".h" part.


[1] I'll qualify this. Narue is always right when it comes to the likes of you. I do make mistakes, but I get the strong impression that you're not a strong enough programmer to notice them or accurately correct me even if you do.

Member Avatar
Eagle-Man
Newbie Poster
3 posts since Sep 2007
Reputation Points: 16 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

1) Yes, I realize that. i just thought I'd post it here for anyone who stumbles across it as I did.

2) It's not that hard for me to understand, and that's why I added the condition "in MinGW". The MinGW compiler supports getch, so anyone using a MinGW compiler will be able to compile the code. After it's been compiled, running the program on the intended platform should have the expected results.

Also, as Cygwin installations typically include MinGW libraries, Cygwin also supports the conio functions like getch. And MS Visual C++ also has the conio library. What compilers don't support it?

3) I'm explaining that cin.get() is not a suitable replacement for getch() for the applications given above.

4) Okay, you got me there. I believe that the ".h" version was the one used before streams became standard (in Borland). Anyway, that's being nit-picky as it was probably just a typo, and anyone would very quickly discover that the ".h" is not needed (since that version isn't included in the installs anymore).

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

>i just thought I'd post it here for anyone who stumbles across it as I did.
It's generally considered rude to resurrect ancient threads, for any reason.

>The MinGW compiler supports getch, so anyone using a <blah blah>
Okay, point out on this thread where you're not the first person to mention a specific compiler or OS. I can describe situations that only work on a PDP-11, and they'd be just as completely irrelevant as you talking about MinGW on this thread. getch is non-portable. That was already mentioned, so you don't have to list the compilers where it does and doesn't work. That's what the manual is for.

>I'm explaining that cin.get() is not a suitable replacement for getch() for the applications given above.
For the application I mentioned it's a perfectly suitable replacement. For masking a password, it's not, but because the OP was okay with using getch after finding out that it's neither C nor C++, but a compiler extension, no replacement was necessary.

>I believe that the ".h" version was the one used before streams became standard
Correct.

>Anyway, that's being nit-picky as it was probably just a typo
I seriously doubt that it was a typo. A lot of newbies use compilers that take pre-standard code. The lack of namespaces and the use of void main makes it extremely likely that iostream.h was meant.

Member Avatar
NetSpider
Newbie Poster
1 post since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#include <iostream>

using namespace std;
void main(){
cout << "hit any key";
cin.get(); // waiting for any input
}

cin.get(); or std::cin.get(); if we don't using namespace
all works fine.

Member Avatar
Ptolemy
Junior Poster in Training
62 posts since Oct 2007
Reputation Points: 33 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

>void main(){
main returns int. Since you can omit the return statement in C++, using int main on top of being correct and portable actually saves you one keystroke. It's a win-win situation. :)

Member Avatar
dNetGuru
Newbie Poster
1 post since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

getch() is also actually available in C++ while it's a compiler extension it might not be available with your compiler etc.
for example if you're using Microsoft VC++ 2005 or later you may find these two functions useful :

int _getch( void );
wint_t _getwch( void );

these methods are locking and thread-safe, non-locking versions of these two functions are available as _getch_nolock and _getwch_nolock !

--
Good Luck !
The dNetGuru

Member Avatar
nomorejunkmail
Newbie Poster
1 post since Oct 2008
Reputation Points: -7 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

>To anyone reading this thread:
You realize the thread is over three years old, yes?

>getch() is still available in the conio.h header file (at least in MinGW).
I'm sure it works just fine on your compiler, but because it's non-standard, that means it's not guaranteed to work on my compiler, or someone else's compiler. Is this that difficult of a concept for you people to grasp?

Found this thread when googling getch, and I had to respond.

The function you're discussing is NOT a "compiler extension". It's a library function. Crap like this is why people think that C/C++ is so difficult. There is a difference between a language feature and a library function. Jesus, learn the language, people!

The C++ Standard Library is not C++! Console I/O functions are not part of the compiler! You can use any library function with any compiler, as long as you compile the library source using your compiler.

Goddamn...No wonder there are so many idiot java programmers.

Member Avatar
rhoit
Junior Poster in Training
85 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

You have always the choice cause it C++

it just direct track of the keyboard using terminal for linux

just add its segment all will work fine..

like clrscr() also does not work for that is you can use the system() function and type the console command

In the program i have define it as "cls" so you can use it directly... as cls ; in the program

If you remove the #define Linux the code will include the Conio and you can use the getch() in any case ...
Just you any old program in windows can run fine now. .if you add this snippet above the code

#include<iostream>

using namespace std;

#define Linux //Remove this for running in windows

#ifdef Linux
	#define cls system("clear")	
	
	#include <termios.h>
	#include <unistd.h>

	int getch();
	int getch()

	{

		struct termios oldt, newt;
		int ch;
		tcgetattr(STDIN_FILENO, &oldt);
		newt= oldt;

		newt.c_lflag &= ~(ICANON| ECHO);
		tcsetattr(STDIN_FILENO, TCSANOW, &newt);
		ch= getchar();
		tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

		return ch;
	}
#else
	#include<conio.h>
	#define cls system("cls")
#endif

#include<stdio.h>


..
..

int main()
{
 ..
 .. 
return 0;
}
Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
1
 

>Found this thread when googling getch, and I had to respond.
I recommend you resist that temptation next time. Posers should know their limits.

>The function you're discussing is NOT a "compiler extension". It's a library function.
If you want to split hairs with terminology, it's a library extension. But whatever you call it, the result is the same: the function is not portable. Calling it a compiler extension is more productive as most beginners can't differentiate between the compiler proper and the libraries. If I say it's a library extension, that will encourage more questions like "I want to use graphics.h with <a compiler it wasn't designed for>".

>The C++ Standard Library is not C++!
That's not quite true. C++ is defined by the C++ standard, and you'll find sections on the standard library in that document. If a compiler package doesn't include the standard library, it doesn't conform to standard C++.

>You can use any library function with any compiler, as long
>as you compile the library source using your compiler.
Perhaps you should try to understand what you're talking about before being a jerk about it. The vendor-provided libraries for a compiler are already compiled. Sometimes vendors provide source code for the libraries, sometimes not. This results in two problems, which are more pronounced in non-portable functions like getch:

1) If you have the source code, chances are extremely good that it uses features, libraries, or APIs that are specific to either the compiler or the platform the compiler is targeted for. Unless you remove those non-portable parts, you can't use "any library function with any compiler, as long as you compile the library source using your compiler". You ultimately have to port the function to the compiler you want to use it with, which could involve a lot more work than simply recompiling.

2) If you don't have the source code, you only have a binary version of the library and thus have to disassemble and reverse engineer the functions you want before you can get to the point of compiling with another compiler. Of course, you still have to handle the first problem after you've done all of this grunt work.

>Goddamn...No wonder there are so many idiot java programmers.
You don't know what you're talking about. A little book knowledge, a pedantic attitude, and arrogance aren't going to hide that from people who do know what they're talking about.

The good news is that posers can become actual authorities on a subject. Keep trying, one day you might actually be able to debate intelligently with me.

Member Avatar
nucleon
Posting Pro in Training
476 posts since Oct 2008
Reputation Points: 114 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 0 [?]
 
-1
 

Narue: "Posers should know their limits."
You say it's rude to resurrect old threads but you're rude whenever you feel like it. One of the rules of this forum is "keep it pleasant". You are not following that rule. It's sad how a relatively simple skill like programming can go to a simpleton's head. Don't bother to respond; I won't read it.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
2
 

>but you're rude whenever you feel like it
I'm appropriately rude. If you're rude to me, don't expect me to be nice to you.

>One of the rules of this forum is "keep it pleasant".
Not only am I quite familiar with the rules, I'm in the unique position of having the final say on their interpretation.

>You are not following that rule.
The key phrase in Keep It Pleasant is "malicious intent", which is the deciding factor in enforcing this rule. If it's determined that there's no malicious intent, even rude posts are more likely to be allowed than not. As such, I don't feel I've broken that particular rule. However, as it appears you've reported my post, I'll step aside and allow the rest of the mod team make an objective decision. Thank you, by the way, for reporting posts you feel cross the line regardless of who the author is. Nobody is exempt from following the rules.

>It's sad how a relatively simple skill like programming can go to a simpleton's head.
If you think programming is simple, you clearly haven't done much of it.

>Don't bother to respond; I won't read it.
I don't believe that for one second. If you're egotistical enough to scold me for not being nice when it has nothing to do with you, you're egotistical enough to be interested in my reply.

To be honest, I was half expecting your IP to match nomorejunkmail's. :D

You
Post:
Start New Discussion
Tags Related to this Article