Created
April 29, 2012 17:31
-
-
Save kdmkdmkdm/2552135 to your computer and use it in GitHub Desktop.
switch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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