Character input and conditional processing

This is a discussion on Character input and conditional processing within the C++ Programming forums, part of the General Programming Boards category; I'm designing a program where the user types in 1 letter commands to do stuff. Code: #include <iostream.h> #include <stdlib.h> ...

  1. #1
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187

    Character input and conditional processing

    I'm designing a program where the user types in 1 letter commands to do stuff.
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {
    char command;
    cout << "Enter command";
    cin >> command;
    But I'm having trouble getting the program to detect what letter is inputted, so that if it is H it displays a helpmessage, or if its Q it quits.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  2. #2
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    Code:
    switch(command)   //command is the variable to test
    case 'h':
    case 'H':
       //code for help
       break;
    case 'q':
    case 'Q':
       //quit
       break;
    case 'c':
    case 'C':
       //do things for 'C'
       break;
    default:
       //catches any other letter
    }
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    State your problem more clearly
    what kind of var youre inputting it in?
    where are your conditional statements?

  4. #4
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    Thanks Madhatter, your code was a big help.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional
    By bazzano in forum C Programming
    Replies: 9
    Last Post: 03-25-2006, 09:36 AM
  2. Reading numeric input one by one
    By oval in forum C Programming
    Replies: 10
    Last Post: 01-24-2006, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21