Connecting Tech Pros Worldwide Forums | Help | Site Map

Pausing screen on LINUX while using C++

Newbie
 
Join Date: Dec 2006
Posts: 3
#1: Dec 29 '06
how do we pause screen on LINUX, till user presses any key..
like we use getch() in WINDOWS & getchar() in UNIX..
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#2: Dec 29 '06

re: Pausing screen on LINUX while using C++


Quote:

Originally Posted by freakykanwar

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
Newbie
 
Join Date: Dec 2006
Posts: 3
#3: Dec 31 '06

re: Pausing screen on LINUX while using C++


i had tried getchar() before posting my problem here.. the program seems not be reacting to this command.. getchar's presence & absence is similar..
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#4: Dec 31 '06

re: Pausing screen on LINUX while using C++


Quote:

Originally Posted by freakykanwar

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
Newbie
 
Join Date: Dec 2006
Posts: 3
#5: Jan 5 '07

re: Pausing screen on LINUX while using C++


Quote:

Originally Posted by horace1

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..
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#6: Jan 5 '07

re: Pausing screen on LINUX while using C++


Quote:

Originally Posted by freakykanwar

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.
Reply