我对编程很新,所以如果这是一个非常无聊的问题我会道歉。我正在创建一个基本的,多选择类型的游戏,但我遇到了一个问题。当我在switch循环中执行我的代码时(在void shop()中),我得到一个常量"这不是一个有效的选项。请再试一次"即使我输入1,2,3或4.如果你帮助我,我将非常感激。谢谢!
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
bool stoleSomething = false;
string choice = " "; //we will use this choice to manipulate leaving buildings and what-not
void shop(void);
void arena(void);
void inn(void);
int main(){
cout << "You enter the town and see three main areas: the Arena, the Shop and the Inn\n";
cout << "Where do you want to go? Type the name of the location that you want to go - (*no caps* 'arena', 'shop' or 'inn'): \n";
while (choice != "arena" && choice != "shop" && choice != "inn"){
cin >> choice;
if (choice == "arena"){
arena();
}
else if (choice == "shop"){
shop();
}
else if (choice == "inn"){
inn();
}
else{
cout << "That is not a valid choice, please try again. \n";
}
}
system("pause");
}
void shop(){
int choice_shop = 0;
static int isDrunk = 0;
cout << "Here are the things that you can do in the shop: \n";
cout << "1. Have a drink \n";
cout << "2. Repair equipment \n";
cout << "3. Steal something \n";
cout << "4. Leave \n";
while (choice_shop != 4){
cout << "Enter the option number of the action that you want to take: ";
cin >> choice;
switch (choice_shop){
case 1: cout << "You feel a bit more tipsy. \n";
isDrunk += 1;
break;
case 2: cout << "your equipment is fully repaired. \n";
break;
case 3: cout << "You hear the shop owner yell at you, and you become frightened; you cannot enter the shop again. \n";
stoleSomething = true;
choice = " ";
break;
case 4: choice = " ";
default: cout << "That is not a valid option. Please try again. \n";
}
if (isDrunk == 5){
cout << "You have drunk so much that you finally black out. \n";
cout << "You wake up just outside of the town. \n";
choice = " ";
}
}
}
void arena(){
//these will be filled later
}
void inn(){
//this will be filled later
}
答案 0 :(得分:1)
您有一个基于while
的{{1}}循环和switch
语句,该语句仍然初始化为choice_shop
。
0
答案 1 :(得分:1)
choice_shop设置为0,您从未更改过它。所以它始终是Switch(choice_shop)的默认情况。 你真的打算用'cin&gt;&gt; choice_shop&#39;在void商店()???
答案 2 :(得分:1)
在输入
之前,您需要让用户修改您的choice_shop变量