replacement of getch()

Reply

Join Date: Jul 2004
Posts: 10
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

replacement of getch()

 
0
  #1
Oct 3rd, 2004
i just want to whether there is a replacement for getch() in C++.

my friend said getch() is C function.

thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,534
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: replacement of getch()

 
0
  #2
Oct 3rd, 2004
>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:
  1. #include <conio.h>
  2.  
  3. int main()
  4. {
  5. // Your program here
  6.  
  7. getch();
  8. }
That's not a recommended solution, and most experienced programmers will recommend that you use cin.get:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. // Your program here
  6.  
  7. std::cin.get();
  8. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: replacement of getch()

 
0
  #3
Oct 5th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 10
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

Re: replacement of getch()

 
0
  #4
Oct 5th, 2004
i'm using getch() at password entering that display * when password is entered.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: replacement of getch()

 
0
  #5
Oct 5th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,300
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 225
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: replacement of getch()

 
1
  #6
Oct 5th, 2004
Originally Posted by Asif_NSU
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
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: replacement of getch()

 
0
  #7
Oct 5th, 2004
Try
#include <iostream.h>
void main()
{ ...... /* your program here */
cin.get();
}

It should work .
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,534
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: replacement of getch()

 
0
  #8
Oct 5th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 3
Reputation: Eagle-Man is an unknown quantity at this point 
Solved Threads: 0
Eagle-Man Eagle-Man is offline Offline
Newbie Poster

Re: replacement of getch()

 
-1
  #9
Sep 28th, 2007
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++.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,534
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: replacement of getch()

 
1
  #10
Sep 28th, 2007
>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.
Last edited by Narue; Sep 28th, 2007 at 4:34 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC