读取用户输入:: cin.get
问题描述:
我已经创建了一个打开控制台,并读取用户的输入的DLL ..读取用户输入:: cin.get
现在我有一个问题,读取用户的输入,当输入是列出的代码调用该函数的功能的价值..
这里是我的代码:
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");
}
}
}
}
}
答
错误使用==操作符来比较两个char * 。编译时,它肯定不会达到你期望的效果,因为它会比较指针的值而不是字符串。
要进行适当的比较,请使用std :: string,或者,如果您必须使用原始C风格的字符串,请使用srncmp()。
+1
肯定这是一个重复并应该像这样关闭。 – NathanOliver
+1
@NathanOliver,我没有问题:) – SergeyA
是什么问题? – NathanOliver
当我在控制台中输入VEHICLE或任何其他列出的字符串时,我得到的是stringstring(“> \ n”).. – 2291eefe
[可以在C++中使用!=和==来进行字符串比较写我自己?](http://*.com/questions/19232814/can-i-use-and-in-c-for-string-comparison-without-writing-my-own) – NathanOliver