LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2015, 03:18 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Making the program to wait for keyboard input.


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?
 
Old 04-12-2015, 03:50 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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.
 
Old 04-12-2015, 05:54 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
The program is this:
Code:
#include <stdio.h>
#include <stdlib.h>

main(){
    char buf[90];
    int n1,n2,i,j,aux;
    
    n1=n2=1; j=2;
    while(1){
        for(i=0; i<10; i++){
            printf("d   %d\n", j,n2);
	    aux= n2;
            n2= n1 + n2;
	    n1= aux;
	    j++;
        }
    


    fflush(stdin);
    getc(stdin);
//    scanf("%s\n", buf);
    } 
}
I mistankenly put the getc outside the while scope. Sorry for that.

Last edited by stf92; 04-12-2015 at 05:55 PM.
 
Old 04-12-2015, 09:49 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by T3RM1NVT0R View Post
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.
Just use 'c', not a literal 99. The compiler generates the same code, and humans can read it more easily.
 
Old 04-12-2015, 11:31 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
never use scanf. fgets + sscanf is the usable way.
 
Old 04-13-2015, 12:24 AM   #6
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
@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.
 
Old 04-13-2015, 05:01 AM   #7
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by ntubski View Post
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
 
Old 04-16-2015, 03:30 PM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read keyboard status (not: wait for keyboard input). stf92 Programming 14 07-03-2011 05:55 PM
Java program needs to quit if no keyboard input for several seconds jeriryan Programming 1 03-29-2011 06:03 PM
C++. cin does not wait for an input by the operator iqbala Programming 7 01-11-2010 12:32 PM
Trouble getting input from USB keyboard in a C++ program called from a SSH shell kungpower13 Programming 1 11-03-2009 07:28 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration