Compass project

Pages: 12
Sep 11, 2013 at 6:09pm
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;
}
Sep 11, 2013 at 6:30pm
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 Sep 11, 2013 at 6:31pm
Sep 11, 2013 at 6:36pm
Thank you for your help.

I also need to add the option Q to quit the program. How would you go about doing that?
Sep 11, 2013 at 9:33pm
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;
}
Sep 12, 2013 at 8:58pm
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?
Sep 12, 2013 at 11:04pm
Put the entire thing inside a while loop with condition while(direction != 'q' && direction != 'Q'). ;)

!= Not equal to

&& - Logical and
Last edited on Sep 12, 2013 at 11:04pm
Sep 13, 2013 at 5:27pm
@Radkowboy it runs until you put in "q" or "Q" then it says "Good bye" and the program ends.
Sep 13, 2013 at 5:31pm
@Mats if you do a while loop how would direction know what the input is?
Sep 13, 2013 at 5:48pm
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.
Sep 14, 2013 at 2:21pm
@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
Sep 14, 2013 at 2:52pm
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" ;
}
Sep 15, 2013 at 3:59am
@JLBorges

Thank you for your suggestion. When I run this program the console window just closes after you enter a direction. Any advice?
Sep 15, 2013 at 12:45pm
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() ;
Sep 15, 2013 at 6:10pm
@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?
Sep 17, 2013 at 12:01am
@Radkowboy

JLBorges's code does do that - look carefully at lines 14 - 17
Sep 18, 2013 at 1:23pm
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 Sep 19, 2013 at 1:36pm
Sep 18, 2013 at 2:59pm
> 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.
Sep 18, 2013 at 4:53pm
*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?
Sep 18, 2013 at 5:20pm
> 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
Sep 18, 2013 at 8:09pm
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 Sep 18, 2013 at 8:10pm
Pages: 12