Making a pause in a "for" loop not working

I'm writing a pretty basic autoclicker for a game... and one of the parts of the program is to retrieve the coordinates, so you can further set where to click...

I have a question asking how many sets of coordinates should be received, a countdown to the coordinate retrieval... display... and then a short pause so you can make sure you get the right thing without having to rush it...

My problem is: it does not pause after the first time going through the coordinates... it goes through the countdown... prints out the coordinates... types the message to click enter... but doesn't wait and simply proceeds forwards... On cycles 2 and afterwards however, it works perfectly fine. Is there any reason to this?

How do I fix this? Maybe just use a different method all around? This is the only way I'm familiar with other than system("pause") though...

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
#include <iostream>
#include <iomanip>
#include <string>
#include <windows.h>
#include <stdlib.h>


using namespace std;

int main()
{
	int repeats, sets;
	POINT cursorPos;
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	cout << "How many times should this be repeated? (0 if unlimited) ";
	cin >> repeats;
	cout << "How many coordinate sets would you like to retrieve? ";
	cin >> sets;
	cout << " " << endl;
	if (sets != 0)
	{
		for (int i = 0; i < sets; i++)
		{
			int loopN;
			loopN = i + 1;
			cout << "Getting mouse position in 3...";
			Sleep(1000);
			cout << " 2...";
			Sleep(1000);
			cout << " 1..." << endl;
			Sleep(1000);
			GetCursorPos(&cursorPos);
			SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY);
			cout << loopN <<  "  X:" << cursorPos.x << " Y:" << cursorPos.y << endl;
			SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
			cout << "Press [Enter] to continue ";
			cin.get();
			cout << "  " << endl;
		}
	}

	cout << " " << endl;
	cin.get();
	return 0;
}
Last edited on
Interesting it appears your input stream buffer is not empty.
You have to clear the buffer then your cin.get() will wait.

Possible solution:
1
2
cin.clear();
cin.ignore(255, '\n');

Not sure if it is a good solution but it works.
Last edited on
Doesn't work... Happens exactly the same as it did before... all runs fine but doesn't stop after the first set... Also what do the (255, '\n') do at cin.ignore?
cin.ignore(255, '\n'); ignores the next 255 character on the input stream unless it is a new line '\n'. http://www.cplusplus.com/reference/istream/istream/ignore/

Well sorry that didn't help but your input stream is not empty and that is the reason cin.get() doesn't work the way you need.
BUMP.
Does anyone who read maybe know a different way of adding a pause?
Yeah getch works, thanks to all who tried to help :)

Would still be interesting to find out why it wouldn't work at first however, if anyone can figure it out.
Topic archived. No new replies allowed.