C++正在检查用户是否输入了有效答案
C++ Checking if a user input a valid answer
当提示时,我需要检查他们是否输入了快乐、悲伤或疯狂:输入心情(快乐、悲伤、疯狂)这是我迄今为止的代码count=0;而(计数<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;
}
}
但如果我输入快乐、悲伤或疯狂,它就会进入无效输入。我还尝试为它们声明字符串变量,比如string happy=快乐;string sad=悲伤;string mad=疯狂;
两件事:
-
确保您正在正确初始化字符串的
string happy = "happy";
-
尝试使用
string.compare()
而不是==
mood.compare(happy);
有关更多详细信息:http://www.cplusplus.com/reference/string/string/compare/
相关文章:
- 文本 RPG - 使用函数检查有效的输入
- 如何检查输入是否有效?
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 如何在使用 cin 请求 int 时有效地使用户输入万无一失?
- 如何知道文本文件中的输入是否是 C++ 中的有效数字
- 拆分字符串输入并查找字符是否有效
- 菜单选择器不接受有效输入
- 如果不有效,如何重复用户输入电子邮件
- 尝试获取用户输入和cout消息期望,当我输入一个有效名称时,它将所有这些命名
- 检查字符串是否与可能的输入之一匹配的最有效方法是什么?
- 循环try..catch ..确保输入有效或使用控制语句
- 测试用户输入数组中的重复项的最有效方法是什么?
- 如何检查字符串输入是否为有效的双精度
- 如何以有效的方式将输入参数传递给函数
- 验证用户输入是否有效 [从 'char' 到 'char*' 的转换无效]
- 我如何让我的部分代码循环并重复一个问题,直到答案是有效的输入C++
- 为什么即使输入有效,也总是设置 cin.failbit
- 输入有效的输入值后仍有提示
- 从C++输入中收集数据时,是否有更有效的方法可以处理变量
- 如果所有输入都有效,则打印总和,否则在 C++ 中打印"Invalid"