DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   replacement of getch() (http://www.daniweb.com/forums/thread11811.html)

Eagle-Man Sep 28th, 2007 4:51 pm
Re: replacement of getch()
 
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).

Narue Sep 28th, 2007 5:06 pm
Re: replacement of getch()
 
>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.

NetSpider Oct 21st, 2007 12:00 pm
Re: replacement of getch()
 
#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.

Ptolemy Oct 21st, 2007 5:30 pm
Re: replacement of getch()
 
>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. :)

dNetGuru Jan 19th, 2008 9:41 am
Re: replacement of getch()
 
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

nomorejunkmail Oct 4th, 2008 6:22 am
Re: replacement of getch()
 
Quote:

Originally Posted by Narue (Post 442426)
>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.

Rhohitman Oct 4th, 2008 10:38 am
Re: replacement of getch()
 
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;
}

Narue Oct 4th, 2008 10:40 am
Re: replacement of getch()
 
>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.

nucleon Nov 2nd, 2008 5:32 pm
Re: replacement of getch()
 
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.

Narue Nov 2nd, 2008 7:51 pm
Re: replacement of getch()
 
>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


All times are GMT -4. The time now is 7:32 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC