Thread: system(PAUSE);
View Single Post
Old 09-26-2004, 08:39 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
ADDENDUM: standard C++

The code below occasionally works...
Code:
#include <iostream>
int main(int argc, char *argv[])
{
  std::cin.get();
  return 0;
}
...but does not suffice.
In that case some do advice to add
Code:
std::cin.sync();
before std::cin.get(); to clear the stream buffer. That might easely not suffice as well. You'll need to add a little more code to make it work after input (e.g. cin>>MyVar). I'll present you the code below wich will work quite effortlessly:
Code:
#include <iostream>
#include <string>

//Namespace std to keep this small code readable.
//However, one shouldn't use the standard namespace globally the
//way I do here.
using namespace std;

void reset_istream(istream & is)
{
   char ch;
   // Reset the state.
   is.clear();
   // Remove all characters until we find a newline or EOF.
   ch = is.get();
   while ((ch != '\n')&&(ch != EOF))
      ch = is.get();
   is.clear();
}

void wait_for_enter()
{
   cout << "press <enter> to continue...\n";
   // Reset failstate, just in case.
   cin.clear();
   string line;
   getline( cin, line);
}

int main()
{
   //Your code here;   

   reset_istream(cout);
   wait_for_enter();

   return 0;
}
This should work in most cases. Notice that sometimes you don't need "reset_istream()" at all. You'll find out soon enough so no worries.

- Val -
__________________
Valmont is offline   Reply With Quote