Hi all in here,

It is said that system("PAUSE"), automatically generated by dev-c++ for console application, to prevent console window of disappearing instantly would be not standard c++, furthermore it would be consume much of a computers resources. What is a good replacement for it?
I have been experimenting with the following code to replace it:

void waitReturn()
{ char c;
cout << "Return to continue...";
c=cin.get();
}

But this mostly requires that the return key has to be pushed twice. Is there any improvement to above function that it behaves just like system("PAUSE")?

Thanks a lot for your advice.
yap

std::cin.get() should work fine.
Here is a small example:

#include <iostream>
#include <string>

int main() {
  std::string foo;
  std::cout << "Enter something: ";
  std::getline( std::cin, foo );

  std::cin.get(); // Should wait for something
  return 0;
}

cin.get()

The unformatted 'get' member function works like the >> operator with two exceptions. First, the get function includes white-space characters, whereas the extractor excludes white space when the ios::skipws flag is set (the default). Second, the get function is less likely to cause a tied output stream (cout, for example) to be flushed.

commented: Good post +3

IMO, interfacing with the user is one of those things that needs to be absolutely bullet-proof.

There are two things to keep in mind here:

1. How does the user expect to interact with your program.
2. How does your program react to insane responses.

Answer 1: For console applications, users expect to have to press ENTER after every input. Just expect it.

Answer 2: Don't crash or do something regrettable.

For example, with just cin.get() (a simple solution for homework, etc), what if the user presses A, N, Y. (That is not a joke. It actually happens, and has had serious consequences!)

So, for this kind of thing, when you are writing production code I always advocate the ignore() method. There is an example of it in the first link VernonDozier gave you.

Keep in mind that if you have been using cin >> foo; up till then, you will have to first empty the input with an additional ignore()...

Hope this helps.

commented: Good post. +3

Thangs a lot guys for fast reply! I will try what VernonDozier has suggested in his first url. Yes Duoas, you are right it couldn't be easy to read a simple 1-byte value because some key are 2-byte coded. My idea with simply getting the char from in-buffer by c=cin.get() could be wrong. I have copied the code Nick posted. May be getline() is better idea, thx. I will try your three suggestions.

Hi yap,
replace cin.get() by these

void waitReturn()
{ char c;
cout << "Return to continue...";
//////////////c=cin.get();
cin.read(&c,1);
if (cin.peek() == 0xA) {;}
}
yap

This works fine for gcc programs.

krs,
tesu

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.