By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,125 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,125 IT Pros & Developers. It's quick & easy.

Pausing screen on LINUX while using C++

P: 3
how do we pause screen on LINUX, till user presses any key..
like we use getch() in WINDOWS & getchar() in UNIX..
Dec 29 '06 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
how do we pause screen on LINUX, till user presses any key..
like we use getch() in WINDOWS & getchar() in UNIX..
try getchar(); it should wait until you hit the ENTER key
Dec 29 '06 #2

P: 3
i had tried getchar() before posting my problem here.. the program seems not be reacting to this command.. getchar's presence & absence is similar..
Dec 31 '06 #3

Expert 100+
P: 1,510
i had tried getchar() before posting my problem here.. the program seems not be reacting to this command.. getchar's presence & absence is similar..
are you reading information from the keyboard (e.g. using scanf()) before attempting to pause. If so the last <enter> key character could be left in the input stream and when you call getchar(); to pause its reads the <enter> and the program carries on. If so you need a second getchar(), e.g.
Expand|Select|Wrap|Line Numbers
  1.     scanf("%d", &i);  // read some data
  2.     getchar();   // read the last <enter> key
  3.     getchar();   // pause for user to hit <enter>
  4.  
the above code works OK under Suse linux 10.1
Dec 31 '06 #4

P: 3
are you reading information from the keyboard (e.g. using scanf()) before attempting to pause. If so the last <enter> key character could be left in the input stream and when you call getchar(); to pause its reads the <enter> and the program carries on. If so you need a second getchar(), e.g.
Expand|Select|Wrap|Line Numbers
  1.     scanf("%d", &i);  // read some data
  2.     getchar();   // read the last <enter> key
  3.     getchar();   // pause for user to hit <enter>
  4.  
the above code works OK under Suse linux 10.1
with fflush(stdin) & two getchar()'s it has started working.. thanks a ton horace..
Jan 5 '07 #5

Banfa
Expert Mod 5K+
P: 8,916
with fflush(stdin) & two getchar()'s it has started working.. thanks a ton horace..
You shouldn't fflush stdin, it is undefined behaviour and thus very bad.

It is in fact undefined behaviour to fflush any input stream or update (read/write) stream whose most recent operation is a read.

Here is the appropriate section of the C standard
int fflush(FILE *ostream);

ostream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment to
be written to the file; otherwise, the behavior is undefined.
Jan 5 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.