[英]Simple c++ input program
我嘗試制作一個很小的基本程序來嘗試使用不是單個數字或字母的輸入。 我輸入了此代碼,它不起作用。 它會啟動並運行,但是當您輸入輸入內容時,它將停止該程序,而無需使用if和else if。 為什么不起作用?
int main()
{
using std::cout;
using std::cin;
using std::endl;
char input[256];
cout << "Is Life Good?\n";
cin >> input;
if (input == "yes") {
}
else if (input == "Yes") {
cout << "Good\n";
}
else if (input == "YES") {
cout << "Good\n";
}
else if (input == "no") {
cout << "Change something\n";
}
else if (input == "No") {
cout << "Change something\n";
}
else if (input == "NO") {
cout << "Change something\n";
}
return(0);
}
input == "yes"
要進行比較,您需要使用strcmp
函數。 ==
運算符不是在比較值,而是在比較指針。
如果使用std::string
則現有代碼將按原樣運行。 std::string
的運算符==
重載,進行比較。
我建議在字符數組上使用std::string
。
==
不能用於比較字符串(它會比較您的情況下的內存地址)或者使用c函數strcmp
或使用C ++ std::string
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.