[SOLVED] Making the program to wait for keyboard input.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Within a loop I want the program to wait the <Enter> key being pressed. I tried with
fflush(stdin);
getc(stdin);
but the control does not stop at the instruction. I don't have my C programs with me, to copy from them (and do have Little practice with C). Could somebody tell me how to accomplish this job?
It will be good if you can share the program, though I am not good at C but here is the program which I wrote and it stops for user input before continuing with the rest of the loop. Basically you will need a condition to stop.
Program:
Code:
#include <stdio.h>
int main()
{
int x;
char a;
for ( x = 0; x <= 10; x++ )
{
if (x == 5)
{
printf("You have reached mid of the loop, press C to continue=");
scanf("d",&a);
if (a == 99)
{
continue;
}
}
printf( "%d\n", x );
}
}
Once it prompt me to hit c to continue and if I enter c it will check ASCII value and compare it against 99 and if it is true it will continue.
@stf92: The third post by you works perfectly in my system.
In linux with glibc waiting for input is the default behaviour unless you use low level terminal functions to return immediately if a key press is not available.
Just use 'c', not a literal 99. The compiler generates the same code, and humans can read it more easily.
Yes that is correct basically I am getting the character input but performing a integer scan which will automatically translate it to 99. Though I haven't code it to work with capital C
Also, please note that fflush serves to flush output buffer to a file. For an input stream, the behaviour is undefined (even though it will probably work on most systems).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.