How do I allow for user input while a for loop is running?

Compiled with no errors (2 warnings) in Visual C++ 2008 Express Edition


I am designing a program that puts a lot of numbers on the screen for a long period of time (i.e. cout<<"7 8 9 " that continues over and over until an integer reaches a certain number), that tests the person's patience to see if they'll sit through all of it or attempt to stop it early.

Only one problem.

A big problem.

I can't figure out how to allow that user to add input to stop the program early. The program will always pause the loop, waiting for the user input.

I'm going to post the full code. The code in question begins at line 163, and ending at line 170 (I'm posting the full code incase you may need some of the info from earlier on or later on in order to help fix it).

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <string>
#include <stdlib.h> // needed for exit (0);

int error = 0;

int input = 0;
int error2 = 0;
int stop;


int patiencetest = 20000;
int patiencetest2 = 1000; // CODE LOOKS GOOD END


using namespace std;

int addition (int x, int y)
{
	int r;
	r=x+y;
	return (r);
}

int subtraction (int a, int b)
{
	int c;
	c=a-b;
	return (c);
}

int multiplication (int d, int e)
{
	int f;
	f=d*e;
	return (f);
}

int division (int g, int h)
{
	int i;
	i=g/h;
	return (i);
}

void sniper()
{
	cout<<"Information loading...\n";
}
void printmessage()
{
	cout<<"Error: error has occured and program must close down.\n";
}

int main()
{
CHECKPOINT1:
	int z; // CODE LOOKS GOOD BEGIN
	int result = 0;
	z = addition (5, 3); // 5+3
	sniper();
	cout<<"What do you think the result will be?\n";
	cout<<"1. 1-10\n2. 11-20\n3. 21-30\n4. 31-40\n5. 41-50\n6. A negative\n7. A positive above 50\n";
	cin>>result;
	switch (result)
	{
		case 1: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 2: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 3: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 4: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 5: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;
		
		case 6: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 7: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		default:
			printmessage();
			error2 = 1;
			break;
	}
	while (error2 == 1)
		system("pause");
	cout<<"\n"; // newline
	cout<<"\n\n\n\n\t\tAbout to begin the patience test.\n";
	cout<<"\n\n\n\n\t\tIf you want to stop this early, enter the number '24'.";
	cout<<"\n\n\n\n\t\tEnter any number to begin.\n"; // requests number input
	cin>>input;
	int beginning = 20000;
	int beginninghelp = 0;
	for (int beginning = 20000; beginninghelp < beginning; beginninghelp++)
	{
		if (input == 0)
		{
			cout<<"0  ";
		}
		if (input == 1)
		{
			cout<<"1  ";
		}
		if (input == 2)
		{
			cout<<"2  ";
		}
		if (input == 3)
		{
			cout<<"3  ";
		}

		if (input == 4)
		{
			cout<<"4  ";
		}

		if (input == 5)
		{
			cout<<"5  ";
		}
		if (input == 6)
		{
			cout<<"6  ";
		}
		if (input == 24)
		{
			cout<<"Program stopped.\n";
			cout<<"\n\n\n\n\t\tEnter \"1\" to restart program.\n\n\n\n\t\tEnter \"2\" to exit entire program.\n\n\n\n\t\tEnter \"3\" \
to skip to end of patience test.\n\n\n\n\t\tEnter \"4\" to go to the code being worked on (if stopearly == 24)";
			cin>>stop;
			if (stop == 1)
				goto CHECKPOINT1;
		    if (stop == 2)
				exit (0);
			if (stop == 3)
				goto CHECKPOINT2;
			break;
			if (stop == 4)
				goto CHECKPOINT3;
			break;
		}
		if (input > 6)
		{
			cout<<"7  8  9  ";
		}
CHECKPOINT3:
	int stopearly;
	cin>>stopearly;
	if (stopearly == 24)
		{
			cout<<"Program stopped early.\n";
			sniper();
		}
	}
	cin.get();
	cout<<"\n\n\n\n\t\tBeginning patience test...\n";
	system("pause");

	 for (int error = 0; error <= patiencetest; error++) 
	{                                                                    

				 cout << ".";

				 if (error == patiencetest)
	{

CHECKPOINT2:
		cout<<"Congratulations! You've passed the patience test. As you've been taught, it's always good to \
wait to see if anything else happens before continuing on.\n";
		error = patiencetest;
				 }
	 }
	 cout<<"\n";
	 cin.get();

	 if ( error < patiencetest)
	 {
		cout<<"NOTE: Error has not been saved to its previous integer numerical value. We are currently unsure of \
how to fix this problem.\n";
		cout<<" Error equals: ";
		cout << error;
		cout<<"\n";
	 }
	 string rebegin;
	 getline(cin, rebegin);
	 if (rebegin == "restart")
	 {
		 goto CHECKPOINT1;
		 cin.get();
	 }
	 cin.get();
}



Any ideas on how to allow the user to input while not pausing the loop at line 109, it's very much appreciated.

Oh, and if you're wondering why I purposefully cause the program to repeat system("pause"), it's to simulate the program shutting down for some type of error. Not needed, just for fun.

Regards,

Warrior2089
Last edited on
Best solution: http://www.cplusplus.com/forum/general/3389/page1.html#msg14326
This just tells you that a key was pressed. It doesn't actually read the key.

Hope this helps.
I would assume (Again I have no idea how to do it) You would need to use an events handler. But that may only limit you to one key stroke (Maybe the enter button)

An article on event can be found here.

http://msdn.microsoft.com/en-us/magazine/cc163659.aspx
Duoas:

I just tried your advice and added the function:

bool iskeypressed()
{
return WaitForSingleObject(
GetStdHandle( STD_INPUT_HANDLE ),
0
) == WAIT_OBJECT_0;
}

into my program, and called the function in the code. However I've gotten 4 errors and I'm not sure on how to organize your code in my program. I'm getting "undeclared identifier" for the following:

STD_INPUT_HANDLE
WAIT_OBJECT_0
WaitForSingleObject
GetStdHandle

My new code, with the function in, is the following:


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <string>
#include <stdlib.h> // needed for exit (0);

int error = 0;

int input = 0;
int error2 = 0;
int stop;


int patiencetest = 20000;
int patiencetest2 = 1000; // CODE LOOKS GOOD END


using namespace std;

int addition (int x, int y)
{
	int r;
	r=x+y;
	return (r);
}

int subtraction (int a, int b)
{
	int c;
	c=a-b;
	return (c);
}

int multiplication (int d, int e)
{
	int f;
	f=d*e;
	return (f);
}

int division (int g, int h)
{
	int i;
	i=g/h;
	return (i);
}

void sniper()
{
	cout<<"Information loading...\n";
}
void printmessage()
{
	cout<<"Error: error has occured and program must close down.\n";
}

bool iskeypressed()
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    0
    ) == WAIT_OBJECT_0;
}

int main()
{
CHECKPOINT1:
	int z; // CODE LOOKS GOOD BEGIN
	int result = 0;
	z = addition (5, 3); // 5+3
	sniper();
	cout<<"What do you think the result will be?\n";
	cout<<"1. 1-10\n2. 11-20\n3. 21-30\n4. 31-40\n5. 41-50\n6. A negative\n7. A positive above 50\n";
	cin>>result;
	switch (result)
	{
		case 1: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 2: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 3: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 4: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 5: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;
		
		case 6: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		case 7: 
			cout<<"The result is " << z; // result of addition (5, 3)
				break;

		default:
			printmessage();
			error2 = 1;
			break;
	}
	while (error2 == 1)
		system("pause");
	cout<<"\n"; // newline
	cout<<"\n\n\n\n\t\tAbout to begin the patience test.\n";
	cout<<"\n\n\n\n\t\tIf you want to stop this early, enter the number '24'.";
	cout<<"\n\n\n\n\t\tEnter any number to begin.\n"; // requests number input
	cin>>input;
	int beginning = 20000;
	int beginninghelp = 0;
	for (int beginning = 20000; beginninghelp < beginning; beginninghelp++)
	{
		if (input == 0)
		{
			cout<<"0  ";
		}
		if (input == 1)
		{
			cout<<"1  ";
		}
		if (input == 2)
		{
			cout<<"2  ";
		}
		if (input == 3)
		{
			cout<<"3  ";
		}

		if (input == 4)
		{
			cout<<"4  ";
		}

		if (input == 5)
		{
			cout<<"5  ";
		}
		if (input == 6)
		{
			cout<<"6  ";
		}
		if (input == 24)
		{
			cout<<"Program stopped.\n";
			cout<<"\n\n\n\n\t\tEnter \"1\" to restart program.\n\n\n\n\t\tEnter \"2\" to exit entire program.\n\n\n\n\t\tEnter \"3\" \
to skip to end of patience test.\n\n\n\n\t\tEnter \"4\" to go to the code being worked on (if stopearly == 24)";
			cin>>stop;
			if (stop == 1)
				goto CHECKPOINT1;
		    if (stop == 2)
				exit (0);
			if (stop == 3)
				goto CHECKPOINT2;
			break;
			if (stop == 4)
				goto CHECKPOINT3;
			break;
		}
		if (input > 6)
		{
			cout<<"7  8  9  ";
		}
	iskeypressed();
	}
CHECKPOINT3:


	cin.get();
	cout<<"\n\n\n\n\t\tBeginning patience test...\n";
	system("pause");

	 for (int error = 0; error <= patiencetest; error++) 
	{                                                                    

				 cout << ".";

				 if (error == patiencetest)
	{

CHECKPOINT2:
		cout<<"Congratulations! You've passed the patience test. As you've been taught, it's always good to \
wait to see if anything else happens before continuing on.\n";
		error = patiencetest;
				 }
	 }
	 cout<<"\n";
	 cin.get();

	 if ( error < patiencetest)
	 {
		cout<<"NOTE: Error has not been saved to its previous integer numerical value. We are currently unsure of \
how to fix this problem.\n";
		cout<<" Error equals: ";
		cout << error;
		cout<<"\n";
	 }
	 string rebegin;
	 getline(cin, rebegin);
	 if (rebegin == "restart")
	 {
		 goto CHECKPOINT1;
		 cin.get();
	 }
	 cin.get();
}



I realize I should probably tinker with it a bit more before asking for further assistance, but I'd like your advice on how exactly I put that into the code for it to call the iskeypressed() function for it to be called, and how exactly I actually declare the identifiers listed above.

Thanks.
You forgot to #include <windows.h>

I'll edit my post on the other page to make the need to do that explicit.

Remember, iskeypressed() is a function returning bool, and it does not actually get the key out of the keyboard buffer. How you do that depends on how you plan to interact with the user... but you can get the single key press with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>

TCHAR getkeypress()
  {
  DWORD mode, count;
  HANDLE h = GetStdHandle( STD_INPUT_HANDLE );
  if (h == NULL) return 0;  // not a console
  GetConsoleMode( h, &mode );
  SetConsoleMode( h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT) );
  TCHAR c = 0;
  ReadConsole( h, &c, 1, &count, NULL );
  SetConsoleMode( h, mode );
  return c; 
  }

Have fun!
Topic archived. No new replies allowed.