Search code examples
c++stringif-statementsubstringuser-input

C++ Two user input keywords in the same line plus separate checking


I've been trying to get an if statement to work with two key words. Basically my program will check the first word as the "command" word (like get, set, etc), the one that will trigger the specific method, and the second word as the one to search in a list, as an object to use. They must be both writen together in the same line.

I know getline can input several words and cin >> this >> that also does it. But for separate checking I can't be able to figure it out. I was advised substrings, but not sure how to do that.

Example: First word as the command, other as the key to search.

if(command == "kick")
{
    std::cin >> input;  //user input "ball"
    person->kick(input) //pointer to function in another class
    ***kicks the ball
}

if(command == "pick")
{
    std::cin >> input; //user input "ball"
    person->pick(input) //pointer to function in another class
    ***picks the ball
}

if(command == "throw")
{
    std::cin >> input;   //user input "paper"
    person->throw(input) //pointer to function in another class
    ***throws the paper
}

It works, but my input is supposed:

kick ball or pick ball

instead of

kick

ball

and so on.

It's probably super simple, I'm just new to c++. It must be in the same line of input though, otherwise there would probably be better ways.

Would love some help. Thanks ;)


Solution

  • If I understand that you need to take both the cmd and key as separate inputs, but the user will enter the string "cmd key" and you need to handle each individually, then a simple method is to read each into a std:string using the normal >> operator, e.g.

        std::string cmd, key;
    
        std::cout << "enter command & key: ";
        if (!(std::cin >> cmd >> key)) {    /* read/validate both cmd & key */
            std::cerr << "stream error or use canceled input.\n";
            return 1;
        }
    

    Now you have both cmd and key stored and can use cmd to initially determine what branch to take and then select what action to take based on key. For example:

        if (cmd == "kick") {        /* handle cmd "kick" */
            if (key == "ball")
                std::cout << "kicking the ball.\n";
            else if (key == "dog")
                std::cout << "kicking the dog.\n";
            else
                std::cout << "generally kicking the " << key << ".\n";
        }
        else if (cmd == "pick") {   /* handle cmd "pick" */
            if (key == "ball")
                std::cout << "picking the ball.\n";
            else if (key == "dog")
                std::cout << "picking the dog.\n";
            else
                std::cout << "generally picking the " << key << ".\n";
        }
        else    /* otherwise unknown command */
            std::cout << "unknown cmd: " << cmd << ".\n";
    

    (note: you always want to handle the case the user has entered something invalid for each cmd or key (or both))

    Putting it together in a short example, you could do:

    #include <iostream>
    #include <string>
    
    int main (void) {
    
        std::string cmd, key;
    
        std::cout << "enter command & key: ";
        if (!(std::cin >> cmd >> key)) {    /* read/validate both cmd & key */
            std::cerr << "stream error or use canceled input.\n";
            return 1;
        }
    
        if (cmd == "kick") {        /* handle cmd "kick" */
            if (key == "ball")
                std::cout << "kicking the ball.\n";
            else if (key == "dog")
                std::cout << "kicking the dog.\n";
            else
                std::cout << "generally kicking the " << key << ".\n";
        }
        else if (cmd == "pick") {   /* handle cmd "pick" */
            if (key == "ball")
                std::cout << "picking the ball.\n";
            else if (key == "dog")
                std::cout << "picking the dog.\n";
            else
                std::cout << "generally picking the " << key << ".\n";
        }
        else    /* otherwise unknown command */
            std::cout << "unknown cmd: " << cmd << ".\n";
    }
    

    Example Use/Output

    $ ./bin/cmd_select
    enter command & key: kick dog
    kicking the dog.
    

    With command "pick":

    $ ./bin/cmd_select
    enter command & key: pick ball
    picking the ball.
    

    With unknown command:

    $ ./bin/cmd_select
    enter command & key: shoot dog
    unknown cmd: shoot.
    

    You can of course pass the string for cmd or key to a separate function and handle your response to each there, but that is just another way of arranging your code to do the same thing. Look things over and let me know if this is what you were intending. I'm still not 100% clear based on the many edits.