Program stuck in a loop

I've been messing around trying to make a program that lets me talk to cleverbot through texting. It uses TextNow to get and send messages and triple clicks messages to see what the newest message is. For some reason it keeps getting stuck in a loop where it just keeps sending the same message over and over. I've been trying to fix it for a while, but can't figure out what's wrong. Anyone have any ideas?

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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
#include <string>
#include <time.h>
#include <shellapi.h>
#include <algorithm>

using namespace std;

void NoFocus() {
	cout << "Could not focus" << endl;
	Beep(200, 200);
	Beep(200, 200);
	Beep(200, 200);
	Sleep(3000);
	exit(EXIT_SUCCESS);
}

void Init(int &currentMin) {
	time_t t = time(0);
	struct tm * now = localtime(&t);
	currentMin = now->tm_min;
}

void SimulateKey(WORD vkey, bool down) {
	INPUT input;
	input.type = INPUT_KEYBOARD;
	input.ki.wScan = MapVirtualKey(vkey, MAPVK_VK_TO_VSC);
	input.ki.time = 0;
	input.ki.dwExtraInfo = 0;
	input.ki.wVk = vkey;
	if (down)
		input.ki.dwFlags = 0;
	else
		input.ki.dwFlags = KEYEVENTF_KEYUP;
	SendInput(1, &input, sizeof(INPUT));
}

void Click(int amount) {
	for (int i = 0; i < amount; i++) {
		INPUT    Input = { 0 };

		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		SendInput(1, &Input, sizeof(INPUT));

		ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		SendInput(1, &Input, sizeof(INPUT));
	}
}

void Copy() {
	SimulateKey(VK_LCONTROL, true);
	SimulateKey('C', true);
	SimulateKey(VK_LCONTROL, false);
	SimulateKey('C', false);
}

void Paste() {
	SimulateKey(VK_LCONTROL, true);
	SimulateKey('V', true);
	SimulateKey(VK_LCONTROL, false);
	SimulateKey('V', false);
}

void SetClipboard(HWND hwnd, const std::string &s){
	OpenClipboard(hwnd);
	EmptyClipboard();
	HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size() + 1);
	if (!hg){
		CloseClipboard();
		return;
	}
	memcpy(GlobalLock(hg), s.c_str(), s.size() + 1);
	GlobalUnlock(hg);
	SetClipboardData(CF_TEXT, hg);
	CloseClipboard();
	GlobalFree(hg);
}

string GetClipboard() {
	OpenClipboard(NULL);
	HANDLE handle = GetClipboardData(CF_TEXT);
	string text = (char *)handle;
	return text;
}

bool FocusTextNow() {
	return SetForegroundWindow(FindWindow(NULL, "TextNow - Google Chrome"));
}

bool FocusCleverbot() {
	return SetForegroundWindow(FindWindow(NULL, "Cleverbot.com - a clever bot - speak to an AI with some Actual Intelligence? - Google Chrome"));
}

void CheckForMessage() {
	if (FocusTextNow()) {
		Sleep(1000);
		SetCursorPos(470, 610);
		Click(3);
		Copy();
	}
	else
		NoFocus();
}

void GetCleverbotResponse(string input) {
	if (FocusCleverbot()) {
		Sleep(1000);
		SetCursorPos(510, 375);
		Click(1);
		SetClipboard(GetDesktopWindow(), input);
		Paste();
		Sleep(100);
		SimulateKey('\r', true);
		Sleep(100);
		SimulateKey('\r', false);
		Sleep(10000);
		SetCursorPos(510, 345);
		Sleep(2000);
		Click(3);
		Sleep(2000);
		Copy();
	}
	else
		NoFocus();
}

void SendCleverbotMessage() {
	if (FocusTextNow()) {
		SetCursorPos(470, 690);
		Click(1);
		Paste();
		Sleep(100);
		SimulateKey('\r', true);
		Sleep(100);
		SimulateKey('\r', false);
	}
	else
		NoFocus();
}

void Shutdown() {
	SendMessage(FindWindow(NULL, "TextNow - Google Chrome"), WM_CLOSE, NULL, NULL);
	SendMessage(FindWindow(NULL, "Cleverbot.com - a clever bot - speak to an AI with some Actual Intelligence? - Google Chrome"), WM_CLOSE, NULL, NULL);
	Beep(300, 150);
	Beep(400, 150);
	Beep(200, 300);
}

int main() {
	atexit(Shutdown);
	srand(time(0));

	ShellExecute(NULL, "open", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "--new-window --start-maximized www.cleverbot.com", NULL, SW_SHOW);
	Sleep(3000);
	ShellExecute(NULL, "open", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "--new-window --start-maximized www.textnow.com", NULL, SW_SHOW);
	system("pause");
	CheckForMessage();
	string lastMessage = GetClipboard();

	while (1) {
		SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)-1);
		Sleep(500);
		if (FocusTextNow()) {
			cout << "Focused TextNow" << endl;
			CheckForMessage();
			string message = GetClipboard();
			cout << "Message: " << message << endl << "Last Message: " << lastMessage << endl;
			if (lastMessage != message) {
				cout << "New Message!" << endl;
				GetCleverbotResponse(message);
				Sleep(1000);
				SendCleverbotMessage();
				Sleep(5000);
				
				lastMessage = message;
				Sleep(500);
			}
			else {
				Beep(600, 500);
			}
		}
		else
			NoFocus();

		Sleep(3000);
		if (GetAsyncKeyState(VK_ESCAPE)) {
			system("pause");
			Beep(300, 300);
			Beep(300, 300);
		}
	}
}
Okay well I've figured out that for some reason the program works fine unless I do the cleverbot stuff. I really don't understand why it wont work. It is supposed to recognize whenever there is a new message and do something only then, but for some reason when I use this function it always thinks that lastMessage is equal to message. Does anyone see a problem with this function? I've been trying to figure it out for a long time...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void GetCleverbotResponse() {
	if (FocusCleverbot()) {
		Sleep(1000);
		SetCursorPos(510, 375);
		Sleep(500);
		Click(1);
		TypeMessage(GetClipboard());
		Sleep(100);
		SimulateKey('\r', true);
		Sleep(100);
		SimulateKey('\r', false);
		Sleep(10000);
		SetCursorPos(510, 345);
		Sleep(2000);
		Click(3);
		Sleep(2000);
		Copy();
	}
	else
		NoFocus();
}
Topic archived. No new replies allowed.