Doing something on push of key.


I need a way for the user to be able to push a button to cause something.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
cout << "Welcome" << endl;
cout << "Push any key to continue..." << endl;
//Right here I want them to be able to push any key, and then the program continues.
cout << "Good Job." << endl;

int x;
cin >> x;
There are two ways to do this. The first requires you to push "enter," not just any key. You can do that with "cin.get()" like this

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
cout << "Welcome" << endl;
cout << "Push any key to continue..." << endl;
//Right here I want them to be able to push any key, and then the program continues.
cin.get();
cout << "Good Job." << endl;

int x;
cin >> x;


The other way may be considered poor programming practice, as it may not work depending on your environment. You can use system("PAUSE") like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
cout << "Welcome" << endl;
cout << "Push any key to continue..." << endl;
//Right here I want them to be able to push any key, and then the program continues.
system("PAUSE");
cout << "Good Job." << endl;

int x;
cin >> x;
Topic archived. No new replies allowed.