如何在std :: string中搜索特定关键字

时间:2018-01-15 00:19:05

标签: c++11

我试图在std :: string中搜索关键字并返回一个布尔值,如下所示;

std::string select_class()
{
    std::string Class;  //Defines the string 'Class'
    std::string user_input;  //Defines the string 'input

    //Tells the user available commands
    std::cout << "Class selection - Available classes = 
    \n\nArcher\nRouge\nWarrior\nMage\n\nCommands: \nset_class - selects your 
    class for this game";

    std::cout << "\ninfo (class_name) - provides information regarding the 
    specific class";

while (true)
{

        std::cin >> user_input;//Asks user to input a command

        if (std::find(user_input,"set_class") == true)//Tests to see if 
        //specific keyword has been inputted
        {
            std::cout << "Please enter your class:";
        }
    }


}

我希望能够在我的while循环中使用if语句返回一个布尔值(或者具有相同的结果)。

对于格式不佳的道歉,因为这是我的第一篇文章。

1 个答案:

答案 0 :(得分:1)

使用string.find,如下所示:

std::cin >> user_input;//Asks user to input a command

if (user_input.find("set_class") != std::string::npos)//Tests to see if 
//specific keyword has been inputted
{
    std::cout << "Please enter your class:";
}