Skip to content

Instantly share code, notes, and snippets.

@kdmkdmkdm
Created April 29, 2012 17:31
Show Gist options
  • Select an option

  • Save kdmkdmkdm/2552135 to your computer and use it in GitHub Desktop.

Select an option

Save kdmkdmkdm/2552135 to your computer and use it in GitHub Desktop.
switch
// giNavigator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int posX = 0;
int posY = 0;
char direction = 'a';
bool quit = false;
while (!quit)
{
cout << "Current Position = (" << posX << ", " << posY << ")" << endl;
cout << "Move (N)orth, (E)ast, (S)outh, (W)est or (Q)uit?";
cin >> direction;
switch(direction)
{
case 'N':
{
++posY;
break;
}
case 'n':
{
++posY;
break;
}
case 'E':
{
++posX;
break;
}
case 'e':
{
++posX;
break;
}
case 'S':
{
--posY;
break;
}
case 's':
{
--posY;
break;
}
case 'W':
{
--posX;
break;
}
case 'w':
{
--posX;
break;
}
case 'Q':
{
quit = true;
cout << "Exiting..." << endl;
break;
}
case 'q':
{
quit = true;
cout << "Exiting..." << endl;
break;
}
default:
{
cout << "You've entered an invalid key." << endl;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment