Demonstrate else...if with char value - C++ Statement

C++ examples for Statement:if

Description

Demonstrate else...if with char value

Demo Code

#include <iostream>
#include <conio.h>              //for getche()

using namespace std;

int main()/*from   ww w .j  av  a  2 s. co  m*/
{
   char dir='a';
   int x=10, y=10;
   cout << "Type Enter to quit\n";
   while( dir != '\r' )         //until Enter is typed
   {
      cout << "\nYour location is " << x << ", " << y;
      cout << "\nPress direction key (n, s, e, w): ";
      dir = getche();           //get character
      if( dir=='n')             //go north
         y--;
      else if( dir=='s' )       //go south
          y++;
      else if( dir=='e' )       //go east
          x++;
      else if( dir=='w' )       //go west
          x--;
   }
   return 0;
}

Result


Related Tutorials