navigator not working

I can't seem to get this code to work. I'm just beginning c++ and this is one of my assignments. For some reason it says the + and - operators won't aren't doing anything.

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
  #include <iostream>
#include <string>
using namespace std;

int main()
{
	int positionArray[2] = { 0, 0 };
	string position = " ";
	bool quit = false;
	cout << "Current position = " << positionArray[ 0, 1] << endl;
	cout << "Move (N)orth, (E)ast, (S)outh, (W)est (Q)uit? ";
	cin >> position;

	while (!quit)
	{
		if (position == "n" || position == "N")
		{
			cout << "Current position = " << positionArray[0, 1] << endl;
			positionArray[1] + 1;
		}
		else if (position == "e" || position == "E")
		{
			cout << "Current position = " << positionArray[0, 1] << endl;
			positionArray[0] + 1;
		}
		else if (position == "s" || position == "S")
		{
			cout << "Current position = " << positionArray[0, 1] << endl;
			positionArray[1] - 1;
		}
		else if (position == "w" || position == "W")
		{
			cout << "Current position = " << positionArray[0, 1] << endl;
			positionArray[0] - 1;
		}
		else if (position == "q" || position == "Q")
		{
			cout << "Exiting..." << endl;
			quit = true;
		}
		else
		{
			cout << "Invalid input, try again: ";
			cin >> position;
			cout << endl;
		}
	}
}
Last edited on
Your problem is that you have an endless loop.

Say someone enters "N". And then what? It will enter the if statement forever, because you dont give the user another chance at choosing a direction so they cant quit, and position will always be equal to "N" because you again, never ask them the question again. Move the cin >> position; to inside the loop.
positionArray[0, 1] is really just the same as positionArray[1]. It accesses the second element in the array.

positionArray[1] + 1; doesn't do anything. If you want to increase the value of the second array element by one you have to do positionArray[1] = positionArray[1] + 1; or positionArray[1] += 1; or you could also do ++positionArray[1];
Last edited on
Thank you both a lot. I only have one more problem, I can't seem to get it to print both elements of the array. What am I doing wrong with that part?

edit: nevermind. I figured that out. I just put it like: cout << positionArray[0] << positionArray[1]. Which ended up solving my problems. thanks again!
Last edited on
Topic archived. No new replies allowed.