I wrote a pause function:

void Pause(void)
{
  printf("Paused...\n\n");
  fgetc(stdin);
}

It generally works fine (hit enter to continue). But one program doesn't seem to want to stop at all...

if(P == 0)
	{
		cout << "P=0!" << endl;
//just as another attempt to stop it , should stop here and ask for input
		int a;
		cin >> a;
//should stop here for input as well
		Pause();
	}

The output is this (and I never hit any keys):

0
P=0!
Paused...

0
P=0!
Paused...
0
P=0!
Paused...

Any idea why this wouldn't be stopping??

Thanks,

Dave

Recommended Answers

All 7 Replies

probably becuase there are still keys in the keyboard buffer. And since you are writinig a c++ program why aren't you using cin and cout? There is a sticky thread at the top of this c++ board that explains how to flush the input buffer of all keys -- you should read it.

So if I look at something like this:

#include <istream>

void ignore_line ( std::istream& in )
{
  char ch;

  while ( in.get ( ch ) && ch != '\n' )
    ;
}

I'd want to turn it into:

#include <istream>

void Pause()
{
  char ch;

  while ( in.get ( ch ) && ch != '\n' );
}

Of course, "in" isn't declared... what would I have to pass Pause() (ie. how do I call it?) so that a pause effect is achieved?

Also, there shouldn't be anything in the input stream because I am doing no other keypressing in the program, right?

David

my previous question still holds (about how to do this the c++ way) - but I figured out that it was my environment (KDevelop) that was producing this very odd behavior - if I run the executable from a terminal it works correctly - so I'm inquiring with the KDevelop people - the good news is that it's not a c++ issue!

It'd still be nice to get all this c style junk (ahem, code) out of my code though!

Dave

In the code you posted "in" should be an istream. Since your taking in input from the keyboard, you can just replace it with "cin"

but the code you posted isn't really a 'pause' function. It's a function to flush the stream so that you can (for example) pause your code woth a cin.get() So

void flush_it()
{
  char ch;
  while ( cin.get ( ch ) && ch != '\n' );
}

int main()
{
  int abc;
  cin >> abc;
  cout << "input was: " << abc;// formatted input
  flush_it(); //flush the input stream
  cin.get(); //pause the program
  return 0;
}

So here's what I've got

void Pause(void)
{
	char ch;
	while ( cin.get ( ch ) && ch != '\n' );
	cin.get(); //pause the program
}

void TestPause()
{
	cout << "hello" << endl;
	Pause();
	cout << "goodbye" << endl;
	
}

It's pretty close... I have to hit enter twice though to continue the program. Is there a way to make it so one "enter" will do it?

Dave

So here's what I've got

void Pause(void)
{
	char ch;
	while ( cin.get ( ch ) && ch != '\n' );
	cin.get(); //pause the program
}

void TestPause()
{
	cout << "hello" << endl;
	Pause();
	cout << "goodbye" << endl;
	
}

It's pretty close... I have to hit enter twice though to continue the program. Is there a way to make it so one "enter" will do it?

Dave

niek's example has input entered, yours doesn't, so the input buffer in his program starts out non-empty when his program hits the first cin.get () , unlike your input buffer, which is empty. That's the difference. Get rid of your second cin.get () . Also, as Ancient Dragon mentioned, there is a stickie at the top of the forum that addresses how to clear the input buffer that may be of interest.

That's because you only need to flush the istream after formatted input. So after a cin >>, you call flush_it() and then cin.get() . But if you haven't used cin >> yet, there's probably nothing on the stream so 1 cin.get() will do the trick without flushing.

you could also save yourself a lot of headache and read the ignore() option in the thread mentioned by AD. It'll do the trick.

[edit]too slow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.