other options for system()

Jun 12, 2011 at 11:29pm
I read that system() was bad to use. Then what should I do if I want the person to hit a key to continue?
(instead of: system("PAUSE"))
Jun 12, 2011 at 11:53pm
Console.ReadKey() or cin.ignore()


1
2
3
4
5
Console.WriteLine("Press Any Key to Continue...");
Console.ReadKey();

std::cout<<"Press Another Key";
std::cin.ignore();

Last edited on Jun 12, 2011 at 11:54pm
Jun 13, 2011 at 2:34am
I use getchar() from the conio.h library.

Example:

1
2
3
4
5
6
7
8
9
#include <conio.h>
#include <iostream>

int main() {
using namespace std;
cout << "Hello World" << endl;
getchar();
return 0;
}
Jun 13, 2011 at 3:59am
for console.readkey() it said that i needed to include something.
and neither cin.ignore() or getchar() worked.
instead of pausing to wait for a key to be pressed it just kept going.
Jun 13, 2011 at 4:45am
You probably did something like this then:

1
2
int x;
std::cin>>x;


The problem is that that leaves junk in the input buffer. (A newline to be exact) You'll need to remove it before you can pause the console with std::cin.ignore();.
Jun 13, 2011 at 4:49am
well how do i remove it then?
even though i dont think i did anything like that.
Jun 13, 2011 at 4:02pm
std::cin.sync() will flush the input stream.
Topic archived. No new replies allowed.