a question about something but I dont know what to call it

This is a discussion on a question about something but I dont know what to call it within the C++ Programming forums, part of the General Programming Boards category; Lets say I had two commands: Code: if (!strcmpi("command 1", command)) if (!strcmpi("command 2", command)) and each of these did ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    a question about something but I dont know what to call it

    Lets say I had two commands:

    Code:
    if (!strcmpi("command 1", command))
    if (!strcmpi("command 2", command))
    and each of these did something very similar, such as

    Code:
    executecommand(1);
    //or
    executecommand(2);
    Is there a way I can instead check to see what number was entered, perhaps:

    Code:
    if ('3'==command[9])
    and then, somehow, convert that 3 from a char to an integer and then do

    Code:
    executecommand(integer);
    Last edited by Noobie; 05-15-2003 at 12:08 AM.
    AIM: MarderIII

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yes, as you mentioned you pick the character with command[8] (note: 8, not 9) then convert it to a number.

    If you have 9 commands:
    Code:
    if((Command[8] >= '1') && (Command[8] <= '9'))
    {
       ExecuteCommand(Command[8] - '0');
    }
    If you have a number in ascii form, subtract '0' and you'll get the 'real' number.
    Last edited by Magos; 05-15-2003 at 04:52 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered Luser
    Join Date
    Apr 2003
    Posts
    17
    Originally posted by Magos
    If you have 9 commands:
    Code:
    if((Command[8] >= 1) && (Command[8] <= 9))
    {
       ExecuteCommand(Command[8] - '0');
    }
    That is slightly incorrect.

    Code:
    if((Command[8] >= '1') && (Command[8] <= '9'))
    would be what'd do the trick.

    Also, if all digits ('0'-'9') are to be utilized, then std::isdigit (<cctype>) can be used instead of this condition.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Whops, my fault
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  2. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  4. function call question?
    By correlcj in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2002, 05:33 PM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM

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