Demonstrate SWITCH with char value - C++ Statement

C++ examples for Statement:switch

Description

Demonstrate SWITCH with char value

Demo Code

#include <iostream>
using namespace std;
#include <conio.h>                             //for getche()
int main()/* w w  w  .  ja  va2  s.c  o m*/
{
   char dir='a';
   int x=10, y=10;
   while( dir != '\r' )
   {
      cout << "\nYour location is " << x << ", " << y;
      cout << "\nEnter direction (n, s, e, w): ";
      dir = getche();                           //get character
      switch(dir)                               //switch on it
      {
         case 'n':  y--; break;                 //go north
         case 's':  y++; break;                 //go south
         case 'e':  x++; break;                 //go east
         case 'w':  x--; break;                 //go west
         case '\r': cout << "Exiting\n"; break; //Enter key
         default:   cout << "Try again\n";      //unknown char
      }
   }
   return 0;
}

Result


Related Tutorials