Stop program when the user presses enter?

My program is working fine but I need to stop it when the user presses enter and I haven't been able to figure out how to do that. Any help would be much appreciated!

Thank you!

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

void placeCursor(HANDLE, int, int);
void displayWords(HANDLE);

int main(void)
{
	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
	
	displayWords(screen);
	
	Sleep(5000);
	
	return 0;
}

/******************************************************
*                     placeCursor                     *
******************************************************/
void placeCursor(HANDLE screen, int row, int col)
{ 						// COORD is a defined C++ structure that
	COORD position; 	// holds a pair of X and Y coordinates
	position.Y = row;
	position.X = col;
	SetConsoleCursorPosition(screen, position);
}

void displayWords(HANDLE screen)
{
	int count = 24;
	int index = 0;
	do
	{
		placeCursor(screen, count, 30);
		cout << "UP" << endl;
		placeCursor(screen, index, 50);
		cout << "DOWN" << endl;
		count--;
		index++;
		Sleep(1000);
	} while (count > 20 || index < 25);
}
At the end of the program or at any moment?
@Delshire I would want you to be able to press enter at any moment to quit.

@Ducas I am not sure. I'll have to look into it more but it looks like it might solve my problem. There is no user input in my program.
I am not a very experimentated C++ user but what about if you enclose your main something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(void)
{
while(not enter pressed){

	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
	
	displayWords(screen);
	
	Sleep(5000);
	
	return 0;

}
}
@Delshire That would be ideal but I'm not aware of anything that can go in the parentheses
You can create another thread with cin.get function. When you press ENTER the code will go further and close main, if you set so. You can use <thread> header and std::exit.
You can use GetAsyncKeyState
Topic archived. No new replies allowed.