Please help with moving char across screen.

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
#include "stdafx.h"


#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
int main(){
	std::string grid = "0000000000\n"
		"0000000000\n"
		"0000000000\n"
		"0000000000\n"
		"0000000000\n"
		"0000000000\n"
		"0000000000";
	char user;
	user = 'G';
	int n = 0;


	std::cout << grid;
	

	while (n != grid.length()){

		if (GetAsyncKeyState(VK_LEFT)){
			
			grid.at(n) = 0;
			n++;
			grid.at(n) = user;
			std::cout << grid;


		}
	}
}

The problem is that the while loop keeps on looping like I keep on clicking the left key , also, there is always a space between the last 0 before the G and the G. Please help.
Last edited on
I don't know Windows programming, so I don't know why you keep getting left-clicks, but I suspect it's because you're checking if the button is down but never make sure it went back up.

However, I do know that you need to be careful about replacing the 0's and G's because you're overwriting your '\n's
The problem is that the while loop keeps on looping like I keep on clicking the left key

That's because the loop executes very quickly. You might add a Sleep(100) or similar inside the loop to slow it so you can see what is happening.

also, there is always a space between the last 0 before the G and the G.
That isn't a space. It's the unprintable character 0 you stored there.
 
    grid.at(n) = 0;
should be
 
    grid.at(n) = '0';

Note the single quotes.

You might also find the gotoxy() function useful here. See this thread:
http://www.cplusplus.com/forum/beginner/4234/#msg18563
Last edited on
Topic archived. No new replies allowed.