简体   繁体   English

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

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

i have created a DLL that opens a console and read the inputs of a user.. 我创建了一个DLL,该DLL可打开控制台并读取用户的输入。

Now i got a problem with reading the input of a user and when the input is an value that is listed with an function the code calls the function.. 现在我在读取用户输入时遇到问题,当输入是函数列出的值时,代码将调用该函数。

Here is my code: 这是我的代码:

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

} }

The error is using operator== to compare two char*. 错误是使用operator ==比较两个char *。 While this compiles, it certainly does not what you expect it to do, since it compares value of the pointer, rather than the string. 在编译时,它肯定不是您期望的,因为它比较指针的值而不是字符串。

To do proper comparison, either use std::string, or, if you must use raw C-style strings, use srncmp(). 要进行正确的比较,请使用std :: string,或者,如果必须使用原始C样式的字符串,请使用srncmp()。

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

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