String Issue

Let me start by warning I am new to C++, but I enjoy figuring things out and adapting as I go.

I've almost completed this program and the last step is to call out the quadrant ("North East", "South East", etc.) To make it versatile I've tried to make it state "North" or "South" depending on the value of yAxis rather than making separate if and else statements for xAxis && yAxis. I get the feeling I'm using strings incorrectly but I'm not sure how else to pull this off simply, I can't seem to find anything on this specific issue.

In short, I need to figure out how to get the yQuad in the last line to display as "North" if yAxis is positive, "South" if yAxis is negative, and "Central" if yAxis is 0.

In addition, if you could explain why what I have doesn't work, it would help me greatly in future programs.

Thanks in advance for your time.

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
#include <iostream>
#include <string>

using namespace std;


int main()
{
	// Initialize x and y
	int xAxis = 0;
	int yAxis = 0;

	std::string yQuad;
	std::string xQuad;

	char direction;
	bool finished;

	// Greeting
	cout << "Welcome to the Compass Program Ver 2.0" << endl;
	cout << "Please don't step on the bugs.\n" << endl;

	do
	{
		cout << "Position is: " << xAxis << "," << yAxis << endl;
		cout << "Choose a direction or Quit\n" << endl;
		cout << "N - North" << endl;
		cout << "E - East" << endl;
		cout << "S - South" << endl;
		cout << "W - West" << endl;
		cout << "Q - Quit" << endl;

		cout << "Direction: ";
		cin >> direction;
		
		// North
		if ((direction == 'N') || (direction == 'n'))
		{
			++yAxis;
			finished = false;
		}
		// East
		else if ((direction == 'E') || (direction == 'e'))
		{
			++xAxis;
			finished = false;
		}
		// South
		else if ((direction == 'S') || (direction == 's'))
		{
			--yAxis;
			finished = false;
		}
		// West
		else if ((direction == 'W') || (direction == 'w'))
		{
			--xAxis;
			finished = false;
		}
		// Quit
		else if ((direction == 'Q') || (direction == 'q'))
		{
			cout << "Thanks for playing!\n"<< endl;
			finished = true;
		}
		// Any other entry
		else
		{
			cout << "I don't understand.\n"<< endl;
			finished = false;
		}
	}while (!finished);
	
	// Setting up quadrant names
	if (yAxis >= 1)
	{
		yQuad('North');
	}
	else if (yAxis <= -1)
	{
		yQuad('South');
	}
	else
	{
		yQuad('Central');
	}
	
	if (xAxis >= 1)
	{
		xQuad('East');
	}
	else if (xAxis <= -1)
	{
		xQuad('West');
	}
	else
	{
		xQuad('Central');
	}
	
	// State new position
	cout << "Position is now: " << xAxis << "," << yAxis << endl;
	cout << "You have reached the " << yQuad << xQuad << "quadrant." << endl;
You don't specify the value of strings like that, you are specifying them as if you are constructing them. Try instead to go like this:
yQuad = "North"
Its easier than you think!

Also, a couple of things: In general, a char is written in single quotes and a string in double quotes. So, this means that your checking for chars is fine, but rather than 'North' you should have "North".

Hope this helps.
Topic archived. No new replies allowed.