Help to fix a code.

Hello im a beginner in C++ trying to make a simple ASCII game but i cant get this work, i want to display my position on screen and change it after a key press but i cant even change it after typing it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
using namespace std;
 
int main ()
{
	int posx=4,posy=12; 
	cout << "Use W,A,S,D keys to move." << endl;
	while (true)
	{
		//after first press clear screen here 
		cout << "Current position: (" << posx << "," << posy << ").";
		char i;  
		cin >> i; 
		if (i='w') {posy+1;} 
		if (i='s') {posy-1;} 
		if (i='d') {posx+1;} 
		if (i='a') {posx-1;} 
	} 
} 
if (i='w') should be if (i=='w'). Same for the others.
= is assignment operator
== is equality operator
Last edited on
Thanks but its still not working, i always get the 4,12 output
Oh I didn't read further after seeing that problem.

1
2
3
4
5
if (i == 'w')
{
//problem here:
     posy+1;
}


"posy+1" doesn't do anything, it doesn't change anything or assign anything.
You want posy += 1; which is the same as posy = posy + 1. Same for the other variations of that.
Last edited on
Thanks again works
Topic archived. No new replies allowed.