about getch()

F

fuzhen

If I want to "press any key to continue" in Windows, I can use getch()
But the getch() isn't a standard C function
So what should I to do in Linux?
 
R

rahul

If I want to "press any key to continue" in Windows, I can use getch()
But the getch() isn't a standard C function
So what should I to do in Linux?

getch() is a non-standard extension which does not echo the character
entered. In standard C, you can use getchar(), getc(), fgetc() ....
All these functions read a character. In Windows, the common practice
is to use getch() for halting the program. Both conio.h and getch()
are non-standard extensions. To be on the safer side, you can always
use getchar() if you are trying to write portable code.
 
P

Peter Nilsson

fuzhen said:
If I want to "press any key to continue" in Windows, I can use
getch()

Why would you want to do that?

Just run your program from a shell/dos window and avoid messy
functions that just clutter your code and render it next to useless
for batch scripts.
 
K

Keith Thompson

Eric Sosman said:
"Press any key other than the one you will press next."

"Press ENTER" is also a candidate.

"Press any key to continue. Press any other key to quit."
 
K

Keith Thompson

fuzhen said:
If I want to "press any key to continue" in Windows, I can use getch()
But the getch() isn't a standard C function
So what should I to do in Linux?

As Peter Nilsson points out, it's often just not necessary,
particularly if the "press any key to continue" occurs just before the
program terminates.

If you want the program to continue immediately after the user presses
*any* key, there's no portable solution, though there are various
non-portable solutions. See question 19.1 of the comp.lang.c FAQ,
<http://c-faq.com/>.

If you'll settle for requiring the user to press <return> (or <enter>,
or whatever you want to call it), you can have a loop that reads and
discards characters until it sees a '\n' (or EOF).
 
A

Antoninus Twink

If I want to "press any key to continue" in Windows, I can use getch()
But the getch() isn't a standard C function
So what should I to do in Linux?

In most Linux shells, terminal input is usually line-buffered, so your
program won't find out about character presses by default until the user
presses return. Your main choices are:

1) fiddle with functions in termios.h to switch the terminal to
unbuffered mode, then call getchar(), then set the terminal back to
buffered mode
2) make a (n)curses application and use getch()
3) settle for "press return to continue" and then do something to read a
line, e.g. system("read");
 
V

vippstar

carriage returns are also an acceptable way to indicate natural breaks
in writing.


Hmm. Why try undefined behaviour?
I think he meant fflush(stdout), so "Press 'return' to continue" is
actually written to the stdout stream.
I also believe that it becomes more clear when you read the rest of
his post (ie, the part you snipped)
 
B

Bill Reid

If you'll settle for requiring the user to press <return> (or <enter>,
or whatever you want to call it), you can have a loop that reads and
discards characters until it sees a '\n' (or EOF).

Yes, and you could call that "loop" "gets()" or "fgets()"...

Sheesh...things are getting progressively worse around here...
 
B

Ben Bacarisse

Antoninus Twink said:
In most Linux shells, terminal input is usually line-buffered, so your
program won't find out about character presses by default until the user
presses return. Your main choices are:

1) fiddle with functions in termios.h to switch the terminal to
unbuffered mode, then call getchar(), then set the terminal back to
buffered mode
2) make a (n)curses application and use getch()
3) settle for "press return to continue" and then do something to read a
line, e.g. system("read");

I wonder if you put the odd suggestions and errors in order to start
an off-topic debate. As before, post number 3 to comp.unix.programmer
to find out more about it.
 
V

vippstar

I wonder if you put the odd suggestions and errors in order to start
an off-topic debate. As before, post number 3 to comp.unix.programmer
to find out more about it.
Wonder? Don't wonder. He's a troll, he was a troll, and he'll always
be a troll.
His posts will *always* seek to troll people. It's simple as that, no
need to complicate it with doubts.
It's a problem when he replies to newbies with off-topic BS, and
newbies reply back, usually taking his advice, suggestions etc.
One option is to always correct the troll. Another would be to flame
newbies for replying to trolls.
I prefer the latter, as the former encourages the troll to keep
posting.
 
K

Keith Thompson

Bill Reid said:
Yes, and you could call that "loop" "gets()" or "fgets()"...

Sheesh...things are getting progressively worse around here...

Neither gets() nor fgets() solves the problem.

fgets() requires an argument specifying the maximum input length. If
the input line exceeds that length, fgets() returns before reading the
new-line; if stdin is line-buffered, additional characters will be
left on the input stream, waiting to be read by the next input
operation. It also requires a pointer to storage for a sequence of
characters that are going to be discarded anyway.

gets() is inherently dangerous, and should never be used.
 
B

Bill Reid

Keith Thompson said:
Neither gets() nor fgets() solves the problem.

fgets() requires an argument specifying the maximum input length. If
the input line exceeds that length, fgets() returns before reading the
new-line; if stdin is line-buffered, additional characters will be
left on the input stream, waiting to be read by the next input
operation. It also requires a pointer to storage for a sequence of
characters that are going to be discarded anyway.

gets() is inherently dangerous, and should never be used.

I said it before and I'll say it again...sheesh...

Use fgets() with say a 512-byte static buffer you keep in a library just
for crap like this, and specifically instruct the user to press RETURN,
and of course you'll still find something to whine about, but in essence
all you're doing is using the "event loop" of fgets() to break out of the
loop and "continue", which is what the OP wanted to do, which is simple
to do, but for some strange reason is IMPOSSIBLE to do here in the
well-padded confines of this newsgroup...

I mean, this is just a step ahead (or below!) the "hello world" level
of programming, but SHEEEEEEESH...it's beyond the newsgroup
"regulars"...and yes I've said it before, but it is beyond me how anybody
with that kind of attitude could ever manage to write any useful
computer code in their life...
 
B

Bill Reid

santosh said:
Remember. Bill has vociferously argued that it's not worth checking
input for end-of-file conditions.

Nope, I didn't argue "vociferously" about that...what I HAVE said
is that it NEVER ends with you loons; if I checked for end-of-file, you'd
find some other apocalyptic event to whine about...

If only I could stop your incessant trolling as easily as just pressing
RETURN...
 
K

Kevin Handy

Keith said:
"Press any key to continue. Press any other key to quit."
This program broke my computer.!!!!

I've pressed the caps lock, the shift, the alt, and the control keys.
Then I tried the num-lock, the print-screen, and the pause buttons.
Nothing happens! You have totally destroyed my computer!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top