Skip to content

Instantly share code, notes, and snippets.

@kdmkdmkdm
Created April 22, 2012 16:29
Show Gist options
  • Select an option

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

Select an option

Save kdmkdmkdm/2465088 to your computer and use it in GitHub Desktop.
switchStatementCPlusPlus
// navigatorProgram.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
// Declare.
int positionX = 0;
int positionY = 0;
char go = 'a';
bool q = false;
while (!q)
{
// Show current position.
cout << "Current Position = (" << positionX << ", " << positionY << ")" << endl;
// Figure out where user wants to go.
cout << "Move (N)orth, (E)ast, (S)outh, (W)est or (Q)uit?" << endl;
cin >> go;
switch( go )
{
case 'N' || 'n':
{
++positionY;
break;
}
case 'E' || 'e':
{
++positionX;
break;
}
case 'S' || 's':
{
--positionY;
break;
}
case 'W' || 'w':
{
--positionX;
break;
}
case 'Q' || 'q':
{
cout << "Exiting..." << endl;
q = true;
break;
}
default:
{
cout << "You have entered an invalid direction." << endl;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment