gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:15:16 25 4
gpt4 key购买 nike

我一直在尝试让 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
}

它有效,但我的输入应该是:

kick ball or pick ball

代替

kick

等等。

这可能非常简单,我只是 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 来初步确定要采取的分支,然后选择要采取的操作基于。例如:

    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% 清楚。

关于C++ 同一行中的两个用户输入关键字加上单独检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58908461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com