C++ C++;这个while循环有什么问题?
我试图从头开始编写一个简单的文本冒险游戏,作为一个编程练习,结果令人惊讶。以下是完整的代码:C++ C++;这个while循环有什么问题?,c++,while-loop,C++,While Loop,我试图从头开始编写一个简单的文本冒险游戏,作为一个编程练习,结果令人惊讶。以下是完整的代码: //Twisty Passages All Alike. //An adventure game designed as a practice exercise. #include <iostream> #include <string> using namespace std; int main() { cout << "\t\tTwisty Passag
//Twisty Passages All Alike.
//An adventure game designed as a practice exercise.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "\t\tTwisty Passages, All Alike!\n";
cout << "\tA text adventure by Jellypox Studios.\n";
//INVENTORY INDEX NUMBERS:
//0. note
//1. hammer
//2.
//ROOM NUMBERS:
//0. living room
//1. storeroom
//Setting up the current room.
int ALL_ITEMS = 10;
string inventory[ALL_ITEMS];
int currentRoom = 0;
string input = "DEFAULT";
//Room 0: living room.
bool haveNote = false;
bool haveHammer = false;
//Room descriptions
if (currentRoom == 0)
{
cout << "You are in the living room.\nThere are doors to the south and west and a staircase to the north.\nYou can see: a table, a cat, a fireplace.\n\n";
}
else if (currentRoom == 1)
{
cout << "You are in the storeroom.\nThe room is a jumbled mass of cupboards, hanging sausages, left-out scraps of food and cockroaches.\nThe only thing of interest here is a large chest in the corner.";
}
//THE LIVING ROOM
while (currentRoom == 0)
{
cin >> input;
//THE TABLE
if (input == "look table" || "look at table")
{
cout << "You see: some bones and scraps";
if (haveHammer = false)
{
cout << ", a hammer";
}
if (haveNote == false)
{
cout << ", a note";
}
cout << ".\n\n";
}
else if (input == "look scraps" || "look at scraps" || "look bones" || "look at bones")
{
cout << "What a mess!\n\n";
}
else if (input == "look note" || "look at note" || "read note")
{
if (haveNote == false)
{
cout << "Pick it up first!\n\n";
}
else
{
cout << "It reads: \"Gone hunting. Will be back soon.\"";
}
}
else if (input == "get note" || "take note" || "pick up note")
{
if (haveNote == false)
{
cout << "Got the note.";
inventory[0] = "note";
haveNote = true;
}
else
{
cout << "You already have the note!\n\n";
}
}
else if (input == "look hammer" || "look at hammer")
{
cout << "It's just an ordinary hammer.\n\n";
}
else if (input == "get hammer" || "take hammer" || "pick up hammer")
{
if (haveHammer == false)
{
cout << "Got the hammer.\n\n";
inventory[1] = "hammer";
haveHammer = true;
}
else
{
cout << "You already have the hammer!\n\n";
}
}
//ELSWHERE IN THE ROOM
else if (input == "look cat" || "look at cat")
{
cout << "It stares up at you irritably.\n\n";
}
else if (input == "look fireplace" || "look at fireplace")
{
cout << "A flickering fire warms the house.\n\n";
}
else if (input == "south" || "go south")
{
//CODE TO MAKE THE DOOR UNLOCKABLE GOES HERE!
cout << "The door is locked.\n\n";
}
else if (input == "west" || "go west")
{
currentRoom = 1;
}
else if (input == "north" || "go north" || "upstairs" || "go upstairs")
{
currentRoom = 2;
}
else
{
cout << "Say what?";
}
}
return 0;
}
我到底弄错了什么?它会为输入的每个单词打印一次“你看:一些骨头…”。如果你输入“敏捷的棕色狐狸跳过了懒惰的狗”,它可能会打印“你看:…”9次。因此,看起来您的“cin>>输入”正在将输入字符串拆分为令牌。你必须弄清楚如何让它读一整行,并把它作为一个字符串交给你;对C++我生疏了,我记不起来了。p>
此外,您还编写了:if(输入=“looktable”| |“looktable”)
这里的问题是“看表”总是正确的,所以表达式总是正确的。您可能希望:例如,如果(输入==“look table”| |输入==“look at table”)这段代码中有输入错误 如果(haveHammer=false) 我想应该是双“=”的。编译不会对你大喊大叫,但逻辑不是你想要的 <>这个代码中有非C++代码,
if (input == "hall..." || "ass" )
在C++中,我们不使用它,尽管它是有效的。应该是
if (input =="hall....") || input == "bbbb")
因为我认为您不会只想比较这些字符串文本的内存地址
(我没有仔细阅读代码,谢谢你的评论。欢迎使用堆栈溢出!让人们发现代码中的错误不是特别有效。你应该使用调试器(或添加打印语句)要隔离问题,请跟踪程序的进度,并将其与预期发生的情况进行比较。一旦两者出现分歧,您就找到了问题。(然后,如果必要,您应该构建一个。)
它是漆黑一片。您很可能会被一只蛴螬吃掉。
这行是问题:如果(haveHammer=false)“谢谢你,Oli,我会试试的。”Beovigt:一个不错的编译器会警告一个条件通常是预期的任务,即使类型是布尔型。<代码>输入<代码>是一个<代码>:ST::String ,所以我们确实在C++中这样做,尽管如果我们想它工作,我们就做<代码>((输入=“Hall”)”(输入= =“ASS”)。
在C++
中我们不使用strcmp。在苏俄strcmp
使用C++
谢谢,这会清除一切!(当然,除了如何让它停止拆分我的字符串。我只需尝试谷歌:)每次查看getline以处理行。
if (input =="hall....") || input == "bbbb")