我是编码新手(从昨天开始(,我正在尝试做这件事,当我输入特定的东西时,我会得到一个自定义输出。
#include <iostream>
using namespace std;
int main()
{
if (cin >> "test") {
cout << "test2";
}
system("pause");
return 0;
}
C++没有运算符匹配这些操作数的操作数类型为:std::istream>> const char [5]
您正在尝试读取字符串文本的输入。
如果要在输入字符串test
的情况下输出字符串test2
,请执行以下操作
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
if (s == "test") {
cout << "test2";
}
system("pause");
return 0;
}
代码中存在语法错误。在if test case
内,您正在声明cin >> "test"
。但这是错误的。
如果要测试用户输入,则需要创建一个变量,并使用cin >> variable
读取该变量。然后,在测试用例中使用该变量。它会是这样的:
#include <iostream>
using namespace std;
int main()
{
String text; //Here, you declare a variable of the type string(text)
cin >> text; //Here, you read the user input, for that variable
if (text == "test") { //here you test if that variable equals "test"
cout << "test2";
}
system("pause");
return 0;
}
只需添加头文件 #include<string>
我希望这对你有所帮助。
std::cin
是在变量(或某些变量(中输入一些东西,但不是为了检查输入的东西是否与你想要的相同。如果你想这样做,请在std::string
变量中输入 thing,然后使用 if
进行检查。
喜欢这个:
std::string str;
std::cin >> str;
if (str == "something") {
//do something
}