/* Write a program that displays a menu which allows the user to move one unit north, east, south or west.
Each time the player enters a selection, update the coordinates of the user and output the current position.
Start the player at the origin of the coordinate system.
Your program’s output should look like the following: */
#include
#include
using namespace std;
int main()
{
char cUserInput;
int x;
int i;
int nPos[4][2]= // Represents positions or coordenates
{
{0,0}, //N
{0,1}, //E
{1,0}, //W
{1,1} //S
};
//Ask user for input
for(i=0; i >=0; ++i)
{
cout << "Move (N)orth, (S)outh, (W)est, (E)ast, (Q)uit? ";
cout << endl;
cin >> cUserInput;
switch(cUserInput) // Assign a value to X in order to make decisions.
{
case ("n"||"N"):
x = 0;
break;
case ("e"||"E"):
x = 1;
break;
case ("w"||"W"):
x = 2;
break;
case ("s"||"S"):
x = 3;
break;
case ("q"||"Q"):
x = 4;
break;
default:
x = 5;
break;
}
if (x = 4){ //desicions based on X value
cout << "Exiting....";
break;
}
else if(x > 4){
cout<< "Please insert a valid position";
continue;
}
else {
cout << "Current position: (";
for(i=0; i <2; ++i){
cout << nPos[x][i];
cout << ",";
}
cout << ")"
cout << endl;
continue;
}
}
}
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (pradius):
In the "switch" statement Im getting an error: duplicate case value, previously used here.
any comments??? Thanks
OpenStudy (espex):
You cannot use conditional statements in a switch. Convert your user input to one case and switch that.
OpenStudy (e.mccormick):
No need to convert. There is another way:
case "n":
case "N":
x = 0;
break;
case "e":
case "E":
etc.
This is a pretty comon method as well. Both work fine.
OpenStudy (anonymous):
yes the above way mentioned is known as fall through
OpenStudy (rsmith6559):
You also can put the code that the if and else if would execute into their appropriate case block.
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (pradius):
Thanks guys. Now I have another problem but I'll work on that.