>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();
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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++.
is standard, 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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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
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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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. :)
Ptolemy
Junior Poster in Training
64 posts since Oct 2007
Reputation Points: 44
Solved Threads: 8
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;
}
Rhohitman
Junior Poster in Training
83 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
>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 ".
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401