Compass point

Hi guys..this is for my assignment. the code that i have attempt to do is, i believe it is wrong since it doesn't display the output that i want instead it display all.

i need to use if-else statement and display compass points (such as East,Northeast etc) based on the X and Y entered by the user. i also need to display "no_where" for X==0 and Y==0.

so this is my code..i don't know whether my logic is true or not
thanks for your help in advance

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

int main()
{
	float X, Y;
	cout << "\t\t\t\t*COMPASS*" << endl << endl;
	cout << "This is a compass for you to search for a place if you are lose." << endl;
	cout << "This compass can only determine between number -10 to 10 of a circle for X and Y." << endl;
	cout << "Enter your X: ";
	cin >> X;
	cout << endl;
	cout << "Enter your Y: ";
	cin >> Y;
	cout << endl;
	if (X == 0)
	{
		if (Y > 0 && Y <= 10)
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: North according to the compass points provided." << endl;
	}
		else if (Y < 0 && Y >= -10)
	{
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: South according to the compass points provided." << endl;
	}
	if (Y == 0)
	{
		if (X > 0 && X <= 10)
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: East according to the compass points provided." << endl;
	}
		else if (Y < 0 && Y >= -10)
	{
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: West according to the compass points provided." << endl;
	}
	if (X > 0 && X <= 10)
	{
		if (Y > 0 && Y <= 10)
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: Northeast according to the compass points provided." << endl;
	}
		else if (Y < 0 && Y >= -10)
	{
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: Southeast according to the compass points provided." << endl;
	}
	if (X < 0 && X >= -10)
	{
		if (Y > 0 && Y <= 10)
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: Northwest according to the compass points provided." << endl;
	}
		else
	{
		cout << "X coordinate: " << X << endl;
		cout << "Y coordinate: " << Y << endl;
		cout << "You are in: Southwest according to the compass points provided." << endl;
	}
	if (X == 0 && Y == 0)
	{
		cout << "No_where" << endl;
	}
	return 0;
}
Last edited on
Compiled and tested:

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

int main()
{
        std::cout << "\t\t\tCOMPASS\n\n\n";
        std::cout << "Enter X and Y coordinates (-10 to 10).\n\n";
    
        float x = 0, y = 0;
    
        std::cout << "Enter X coordinate: " << std::flush;
        std::cin >> x;
        std::cout << "Enter Y coordinate: " << std::flush;
        std::cin >> y;
        std::cout << '\n';
    
        std::cout << "X coordinate: " << x << '\n';
        std::cout << "Y coordinate: " << y << '\n';

        if ((x < -10 || x > 10) || (y < -10 || y > 10))
        {
                std::cout << "No where\n";
                return 1;
        }
        if (x == 0 || y == 0)
        {
                if (x == 0 && y == 0)
                {
                        std::cout << "No where";
                        return 1;
                }
                if (x == 0)
                {
                        std::cout << ((y < 0) ? "South" : "North");
                }
                else
                {
                        std::cout << ((x < 0) ? "West" : "East");
                }
        }
        else
        {
                if (x < 0)
                {
                        std::cout << ((y < 0) ? "SouthWest" : "NorthWest");
                }
                else
                {
                        std::cout << ((y < 0) ? "SouthEast" : "NorthEast");
                }
        }
        std::cout << '\n';
    
        return 0;
}

I used the conditional ternary operator here but if you strictly need to use if-else statements then you can replace them with if-else statements.

Syntax for conditional ternary operator:

(condition) ? when_true : when_false
Last edited on
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
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

const double PI = 3.14159265358979323846;
const double SMALL = 1.0e-30;
const double LARGE = 10.0;

int main()
{
   double x, y;
   double r, theta;
   int num;
   string sector[8] = { "East", "NorthEast", "North", "NorthWest", "West", "SouthWest", "South", "SouthEast" };

   cout << "Input x and y: ";   cin >> x >> y;
   r = sqrt( x * x + y * y );

        if ( r < SMALL ) cout << "Nowhere" << endl;
   else if ( r > LARGE ) cout << "Out of range" << endl;
   else
   {
      theta = atan2( y, x ) * 180.0 / PI;   if ( theta < -22.5 ) theta += 360.0;
      num = theta / 45.0 + 0.5;
      cout << "Sector is " << sector[num] << endl;
   }
}

Topic archived. No new replies allowed.