Stop the countdown

I got this program that counts down from a number (specified by the user) to zero, while pausing in between each number for 2 seconds. How would I be able to tell the program to stop counting down if the user presses a certain key, ESC for example, while the countdown is in progress?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

int main()
{
	cout << "Please enter in a number:" << endl;
	int nX;
	cin >> nX;
	cout << "\n";

	cout << "Missles launching in:" << endl;

	for (nX; nX > 0; nX--)
	{
		cout << nX << "\n ";
		Sleep(2000);
	
	}

	cout << "Launching missles!" << endl;
	return 0;
}
You could create two threads; one listens to the keyboard whilst the other executes your countdown.

http://en.wikipedia.org/wiki/Thread_(computer_science)

Edit: r0shi's hack is quite nice, though :)
Last edited on
Topic archived. No new replies allowed.