[英]C++ Checking if a user input a valid answer
I need to check if they input happy,sad or mad when prompted with: input a mood (happy,sad,mad) This is my code for it so far count = 0;我需要检查他们是否在提示时输入快乐、悲伤或生气:输入情绪(快乐,悲伤,疯狂)这是我到目前为止的代码 count = 0; while (count<1){
而(计数<1){
cout << ", How are you feeling today? (happy,sad,mad)" << endl;
cin >> mood;
if (mood == sad){
cout << "im sorry you're feeling sad" << endl;
count++;
}
else if (mood == happy) {
cout << "I'm glad you're feeling happy!" << endl;
count++;
}
else if (mood == mad){
cout << "Don't be angry, there's so many happy things in life!" << endl;
}
else{
cout << " invalid input " << endl;
count = 0;
}
}
but if i put in happy,sad or mad it just goes to the invalid input.但如果我输入快乐、悲伤或生气,它只会进入无效输入。 i also tried declaring string variables for them like string happy=happy;
我还尝试为它们声明字符串变量,例如 string happy=happy; string sad=sad;
字符串悲伤=悲伤; string mad=mad;
字符串疯狂=疯狂;
Two things:两件事情:
Make sure you're initializing your String's correctly确保您正确初始化您的字符串
string happy = "happy";
Try using string.compare()
rather than ==
尝试使用
string.compare()
而不是==
mood.compare(happy);
For more details: http://www.cplusplus.com/reference/string/string/compare/更多详情: http : //www.cplusplus.com/reference/string/string/compare/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.