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. 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?
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.
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?
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?
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."
#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() ;
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?