C ++同一行中的两个用户输入关键字以及单独的检查

我一直在尝试使用if语句来使用两个关键字。基本上,我的程序会将第一个单词作为“命令”单词(如get,set等)进行检查,将触发特定方法的单词作为检查对象,将第二个单词作为在列表中进行搜索的对象进行检查。它们必须同时写在同一行中。

我知道getline可以输入多个单词,而cin >> this >> that也可以输入。但是对于单独的检查,我无法弄清楚。 有人建议我使用子字符串,但不知道该怎么做。

示例: 第一个单词是命令,另一个是搜索关键字。

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
}

可以,但是我的输入应该是

  

踢球或   接球

代替

  

以此类推。

这可能非常简单,我刚接触c ++。 不过,它必须在同一行输入中,否则可能会有更好的方法。

希望得到一些帮助。 谢谢;)

zhangzhiwei1986913 回答:C ++同一行中的两个用户输入关键字以及单独的检查

如果我了解到您需要同时使用cmdkey作为单独的输入,但是用户将输入字符串"cmd key",则需要分别处理每个字符串,然后一种简单的方法是使用常规std:string运算符(例如

)将它们读入>>
    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;
    }

现在,您同时存储了cmdkey,可以使用cmd最初确定要采取的分支,然后根据key选择要采取的行动。例如:

    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";

注意:,您始终希望处理用户为每个cmdkey(或两者)输入无效内容的情况

在一个简短的示例中将其放在一起,您可以:

#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";
}

使用/输出示例

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

使用命令"pick"

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

使用未知命令:

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

您当然可以将cmdkey的字符串传递给一个单独的函数,并在该函数中处理您对每个函数的响应,但这只是另一种安排代码以完成相同操作的方式。仔细检查一下,让我知道这是否是您想要的。根据许多修改,我仍然不是100%清楚。

本文链接:https://www.f2er.com/3083428.html

大家都在问