Reading a single character without waiting for enter key.

J

Johnsy Joseph

Hello Everybody,

I am trying to write a program that reads a single character, but I
always need to press the enter key too. Is there any way to read just
a single character without waiting for the enter key to also be
pressed?

Thanks for the help and time.

Warm Regards
:) Johnsy
 
E

Eric Sosman

Johnsy said:
Hello Everybody,

I am trying to write a program that reads a single character, but I
always need to press the enter key too. Is there any way to read just
a single character without waiting for the enter key to also be
pressed?

This is Question 19.1 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
Thanks for the help and time.

You'd be more welcome if you had read the FAQ first.
Hint, hint.
 
A

Al Bowers

Johnsy said:
Hello Everybody,

I am trying to write a program that reads a single character, but I
always need to press the enter key too. Is there any way to read just
a single character without waiting for the enter key to also be
pressed?

Review faq question 19.1 for an explanation and possible solution.
19.1 is located at:

http://www.eskimo.com/~scs/C-faq/q19.1.html
 
J

Joona I Palaste

Johnsy Joseph said:
Hello Everybody,
I am trying to write a program that reads a single character, but I
always need to press the enter key too. Is there any way to read just
a single character without waiting for the enter key to also be
pressed?

C *does* read characters without waiting. It's your operating system
that waits for Enter. Unfortunately, there is no standard way to make
it stop waiting.
 
L

Lawrence Kirby

C *does* read characters without waiting. It's your operating system
that waits for Enter.

While true at some level, the C standard makes no distinction between "C"
and an "operating system", they are both parts of and contribute to the
implementation. If the OS does something that is incompatible with the C
standard then it is the responsibility of the C package to make
appropriate adjustments/translations to provide a correct environment for
the program to run in. An example is text streams on an OS that
standardises something other than newline terminated lines.

The issue here is that blocking behaviour on input is NOT inconsistent
with the C standard, which makes no requirements about when a read
operation should complete. If it were inconsistent then the C package
would have to deal with the problem.
Unfortunately, there is no standard way to make
it stop waiting.

True, because the language designers chose not to address the issue, but
they could have.

Lawrence
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top