繁体   English   中英

从std :: cin.get读取用户输入

[英]Reading user input from std::cin.get

我创建了一个DLL,该DLL可打开控制台并读取用户的输入。

现在我在读取用户输入时遇到问题,当输入是函数列出的值时,代码将调用该函数。

这是我的代码:

void UserTest::Menu() {
char UserInput[256];

centerstring(" <<- Functions ->>\n\n");

centerstring("<<-  VEHICLE  ->>\n");
centerstring("<<-  KEYBIND  ->>\n");
centerstring("<<-  EXECUTE  ->>\n");
centerstring("<<-  CLEAR    ->>\n");

std::cin.clear();
std::cin.sync();

std::cin.get(UserInput, 256);

if (UserInput == "CLEAR"){
    UserTest::ClearConsole();
    UserTest::Menu();
}else{
    if (UserInput == "VEHICLE"){
        centerstring("<<-  VEHICLE    ->>\n");

        UserTest::PreCallVehicle(UserInput);
    }else{
        if (UserInput == ("EXECUTE")){
            centerstring("<<-  SCRIPT    ->>\n");

            UserTest::PreCallScript(UserInput);
        }else{
            if (UserInput == "KEYBIND"){
                centerstring("<<-  KEYBINDS    ->>\n\n\n");

                UserTest::PreCallKeybind();
            }else{
                UserTest::ClearConsole();
                centerstring("<<-  ERROR    ->>\n");
            }
        }
    }
}

}

错误是使用operator ==比较两个char *。 在编译时,它肯定不是您期望的,因为它比较指针的值而不是字符串。

要进行正确的比较,请使用std :: string,或者,如果必须使用原始C样式的字符串,请使用srncmp()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM