Compass project

Pages: 12
Write your question here.
I am new to c++. I have only written two programs. I have an assignment for my c++ class which consist of creating a program that asks the user to input a direction of (E) for east, (S) for south, (W) for west and (N) for north. The program must accept upper and lower case letters. When they input a direction the program needs to output a coordinate value, for example for E it is (1,0), for (S) it is (0,-1), for (W) it is (-1,0) and for N it is (0,1).

Can anyone help me start this program?

I have made an attempt below but I believe it is all wrong.

Thank you for your help

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

int main()
{

// Simple form of IF statement
int direction;

cout << "\nEnter your direction:  N for north, E for east, S for south W for west" << endl;
cin >> direction;
char letter;
cout << "N =n";
cin>> letter;


// Nesting
if (direction == 'N')
{
    cout << "(0,1)" << endl;
}	
else if (direction == 'E')
{
	cout << "(0,1) "<< endl;
}
else if (direction == 'S')
{
	cout << "(0,-1) "<< endl;
}
else if (direction == 'W')
{
	cout << "(-1,0) "<< endl;
}
	
int pause;
cin >> pause;
return 0;
}
try this

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

int main()
{

// Simple form of IF statement
char direction;
cout << "\nEnter your direction:  N for north, E for east, S for south W for west" << endl;
cin >> direction;

// char letter;
// cout << "N =n";
// cin>> letter;


// Nesting
if ((direction == 'N') || (direction == 'n'))
{
    cout << "(0,1)" << endl;
}	
else if ((direction == 'E') || (direction == 'e'))
{
	cout << "(1,0) "<< endl;
}
else if ((direction == 'S') || (direction == 's'))
{
	cout << "(0,-1) "<< endl;
}
else if ((direction == 'W') || (direction == 'w'))
{
	cout << "(-1,0) "<< endl;
}
else
{
	cout << "I think your lost" << endl;
}
	
// int pause;
// cin >> pause;
return 0;
}
Last edited on
Thank you for your help.

I also need to add the option Q to quit the program. How would you go about doing that?
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
#include <iostream>
using namespace std;

int main()
{

// Simple form of IF statement
    char direction;
    do
    {
        cout << "\nEnter your direction:  N for north, E for east, S for south W for west" << endl;
        cin >> direction;

// char letter;
// cout << "N =n";
// cin>> letter;


// Nesting
        if ((direction == 'N') || (direction == 'n'))
            cout << "(0,1)" << endl;
        else if ((direction == 'E') || (direction == 'e'))
	        cout << "(1,0) "<< endl;
        else if ((direction == 'S') || (direction == 's'))
            cout << "(0,-1) "<< endl;
        else if ((direction == 'W') || (direction == 'w'))
            cout << "(-1,0) "<< endl;
        else
            if(direction == 'Q' || direction =='q')
            break;
            else
            cout << "I think your lost" << endl;
    }while(direction!='Q' && direction != 'q');
    cout<<"Good bye!"<<endl;
// int pause;
// cin >> pause;
    return 0;
}
Thank you for your help. When I run the about program it says good bye after user input and only allows for user input once. Shouldn't it ask again for the direction until the user enters Q?
Put the entire thing inside a while loop with condition while(direction != 'q' && direction != 'Q'). ;)

!= Not equal to

&& - Logical and
Last edited on
@Radkowboy it runs until you put in "q" or "Q" then it says "Good bye" and the program ends.
@Mats if you do a while loop how would direction know what the input is?
Just want to say that I really hate dislike constructs like this:

while(direction != 'q' && direction != 'Q')

IMO they are ugly, messy, error prone & non-scalable - especially as part of a do loop.

@Radkowboy

I think you would be much better to do this instead:

http://www.cplusplus.com/forum/beginner/99203/#msg534078


It is not an entire program, but should be able to work how to adapt it to your needs. The switch keeps things tidy & scalable, and there is a default case to catch bad input.

Also, make use of the toupper or tolower functions, to cut down the logic of having to test everything twice.

http://www.cplusplus.com/reference/cctype/toupper/



Hope all goes well.
@Chriscpp
@TheIdeasMan
@Mats
@SamuelAdams

Guys thank you for all your help.

I asked my instructor about your ideas, in particular @Chriscpp's program and she said I was making it too complicated.
She said "Use variables for your coordinates instead of hard coding them. Forget about the loop -- that is the next assignment."

So how would I go about doing it this way? Any ideas?

Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    int x = 0 ;
    int y = 0 ;

    std::cout << "enter direction:  N (north), E (east), S (south), W (west): " ;
    char direction ;
    std::cin >> direction ;

    switch(direction)
    {
        case 'N' : case 'n' : ++y ; break ;
        case 'E' : case 'e' : ++x ; break ;
        case 'S' : case 's' : --y ; break ;
        case 'W' : case 'w' : --x ; break ;
    }

    std::cout << '(' << x << ',' << y << ")\n" ;
}
@JLBorges

Thank you for your suggestion. When I run this program the console window just closes after you enter a direction. Any advice?
Add the following as the last lines of main:

1
2
3
// wait for the user to press enter before quitting
std::cout << "press enter to quit:" ;
std::cin.ignore( 1000, '\n' ).get() ;
@JLBorges

Your program does not
a coordinate values for the direction, for example for E it is (1,0), for (S) it is (0,-1), for (W) it is (-1,0) and for N it is (0,1). How would you do that?
@Radkowboy

JLBorges's code does do that - look carefully at lines 14 - 17
Could you guys help me with the next step. I need to revise the below program to that it will let the user know which quadrant they are in (Northeast, Southeast, Southwest, Northwest) If they are on an axis, simply state that they are (North, South, East, or West). Example: If the user moves so that coordinates are (1,3), they are in the Northeast quadrant. Make the program continue in a loop until the user types in "Q."

Any thoughts on how to do this?

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
#include <iostream>
//directive, which tells the preprocessor to include the contents of another file.

int main()
    //This is the starting point of the program
{
    int x = 0 ;
    int y = 0 ;
    // sets up x and y intial values
   
    std::cout << "enter direction:  N (north), E (east), S (south), W (west) or Q to pause: " ;
    //cout is an object, defined in the file iostream, that’s used to send data to the standard out put screen
    //sends the string of text to the console window

    char direction ;
    //single character variable type

    std::cin >> direction ;
    //cin is used to get a string, only works with strings that have no whitespace in them

    switch(direction)
        //switch statement to create multiple branches for the different directions
    {
        case 'N' : case 'n' : ++y ; break ;
            //Adds 1 to the y direction for north
        case 'E' : case 'e' : ++x ; break ;
            //adds 1 to the x direction for east
        case 'S' : case 's' : --y ; break ;
            //subtracts 1 from the y direction for south
        case 'W' : case 'w' : --x ; break ;
            //subtracts 1 from the x direction for west
        case 'Q' : case 'q' : system ("pause"); break ;
            //pauses on q
    }

    std::cout << '(' << x << ',' << y << ")\n" ;
    //sends interval for x and y value of direction
    std::cout << "Press enter to quit:" ;
    std::cin.ignore( 1000, '\n' ).get() ;

}
Last edited on
> Make the program continue in a loop until the user types in "Q."

So, start by writing the loop. That is the next step; first get that working.

Once that is done, add the code for: let the user know which quadrant they are in.
*JLBorges

Thank you for your help.

What I am trying to do is prompt the user "enter direction: N (north), E (east), S (south), W (west) or Q to pause:"
when they enter E, The program outputs (1,0) and then prompts:"enter direction: N (north), E (east), S (south), W (west) or Q to pause:" again
If the user again enters E the output would be (2,0) and so on.

Can I loop the case statements above to do this?
Could someone show me an example of a loop of one of the directions doing this?
> Could someone show me an example of a loop

Thousands of examples of while loops: http://www.google.com/search?q=while+loop+site:cplusplus.com
The link I provided much earlier shows a loop, and could be just what you need.

Here it is again.

http://www.cplusplus.com/forum/beginner/99203/#msg534078
Last edited on
Pages: 12