【发布时间】:2017-01-03 05:45:50
【问题描述】:
(使用 dev c++)由于我对 c++ 很陌生,我决定制作一个基本的文本 RPG 游戏。我已经多次调试该程序,但它什么也没有出现。每次我测试程序时,假设我想攻击旅行者,它会打印 TEST(我暂时放在那里,直到我可以解决问题)。如果我将 if(attack=="talk"||"Talk") 与 if(attack=="attack"||"Attack") 切换,那么我想与旅行者交谈,它会打印“你冲上前去谋杀旅行者发现他没有武装!”所有帮助将不胜感激。
#include <iostream>
#include <string>
using namespace std;
int main() {
string dir, attack, trade;
string inventory[] = {"Food", "Sword", "Armor"};
cout << "Do you go left, right or forward?" << endl;
cin >> dir;
if(dir=="left"||"Left") {
cout << "You decide to go left" << endl;
cout << "On the way you meet a traveller, do you attack or talk?" << endl;
cin >> attack;
if(attack=="talk"||"Talk") {
cout << "TEST" << endl;
}
else if(attack=="attack"||"Attack") {
cout << "You rush forward and murder the traveller to find he was un-armed!\a" << endl;
}
else {
cout << "test" << endl;
}
}
else if(dir=="Right"||"Right") {
cout << "You decide to go right" << endl;
}
else {
cout << "You decide to go forward" << endl;
}
system("PAUSE");
return 0;
}
【问题讨论】:
-
将其更改为
dir=="left"||dir=="Left"。否则它将永远是true。 -
在互联网上搜索“c++ 转换字符串”。如果在比较之前将字符串全部转换为大写或全部小写,则只需进行一次比较。
-
顺便说一句,您在检查“正确”方向时有错字;两个比较文本相同。
-
你的逻辑有缺陷。如果我在你的提示下输入“burrito”,它会报告我要继续,这是错误的。现在是开始考虑如何处理无效输入的好时机。