我刚刚开始学习C ++,正在为一个基于文本的冒险游戏制作一个小游戏模板的东西,并希望得到一些帮助。其中一个是排除答案,另一个是在一个人回答并输出后弹出新问题。我会咨询我正在观看的教程的创建者,但他从未超过第2部分,并且在3年内没有触及他的youtube频道。我尝试使用=!
代替==
来获取答案并替换输入内容。我一直在使用在线编译器(http://cpp.sh)来加快编辑速度,所以这里是崩溃日志:
23:25: error: no match for 'operator||' (operand types are 'bool' and 'std::string {aka std::basic_string<char>}')
23:25: note: candidate is:
23:25: note: operator||(bool, bool) <built-in>
23:25: note: no known conversion for argument 2 from 'std::string {aka std::basic_string<char>}' to 'bool'
23:43: error: no match for 'operator||' (operand types are 'bool' and 'std::string {aka std::basic_string<char>}')
23:43: note: candidate is:
23:43: note: operator||(bool, bool) <built-in>
23:43: note: no known conversion for argument 2 from 'std::string {aka std::basic_string<char>}' to 'bool'
23:60: error: no match for 'operator||' (operand types are 'bool' and 'std::string {aka std::basic_string<char>}')
23:60: note: candidate is:
23:60: note: operator||(bool, bool) <built-in>
23:60: note: no known conversion for argument 2 from 'std::string {aka std::basic_string<char>}' to 'bool'
这是我的代码:
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
string input;
int hp = 100;
int finalhp;
cout << "Your HP: " << hp << endl;
cout << "Walking in the DANK castle dungeon, you stumble upon a dying man. Would you like to finish him off?\n1. Yes\n2. No" << endl;
cin >> input;
if (input == "yes" || input == "Yes") {
finalhp = hp - 5;
cout << "You monster, you should have helped him. You are attacked by guilt. -5 HP. You are now at:" << finalhp << endl;
}
else if (input == "no" || input == "No"){
finalhp = hp + 5;
cout << "You spared the man's life. While he sits there peacefully recovering, he gives you a potion giving you 5 hp. You are now at: " << finalhp << endl;
}
else if (input =! "yes" || input =! "Yes" || input =! "no" || input =! "No"){
cout << "Quit babbling like an idiot and answer my question!";
return 0;
}
}
任何帮助都会很棒!
答案 0 :(得分:0)
无数错别字。
input =! "yes" || input =! "Yes" || input =! "no" || input =! "No"
^^ ^^ ^^ ^^
应该是
input != "yes" || input != "Yes" || input != "no" || input != "No"
如果你看operator precedence,表达式
input =! "yes" || input =! "Yes" || input =! "no" || input =! "No"
会非常疯狂,而不是你想到的简单表达。
答案 1 :(得分:0)
=!
不是C ++中的运算符。不等于运算符是!=
。因此,您的最后else if
条件没有按照您的想法进行。它基本上是在做
input = (!"yes" || input = (!"Yes" || input = (!"no" || input = !"No")))
导致您的错误,即operator||(bool, std::string)
已存在。