不显示列表中选定的选项
Not displaying a chosen selection from a list?
所以我被要求在我的c++课上制作一款游戏,我之前上过c++课,所以我有点生疏了。我记得你可以使用数组和列表?
游戏的重点是选择一个军队派系,选择军队类型,然后攻击某人。一旦我进入攻击部分,而不是制作一堆if语句来检查从之前的菜单中选择的阵营,我是否可以从程序的开始将派系实现到数组中,一旦我进入攻击阶段,运行该数组并将用户选择的大陆与数组中的大陆进行比较,然后保留匹配的输出?
如果有人可以发布一个小代码或链接到一个网站,给出一个类似这样的例子,这将是非常感激的,我想自己做,我真的没有完全为我打字的东西。
#include <iostream>
using namespace std;
int main() {
// Declare variables
int menuInput = 0, continentInput = 0, armyInput = 0, actionInput = 0;
// Intro
cout << "WELCOME TO WAR" << endl;
// Display main menu
do {
cout << "1) Rules" << endl;
cout << "2) Play Game" << endl;
cout << "3) Quit" << endl;
cout << "Menu choice: ";
cin >> menuInput;
// if rules is selected
if (menuInput == 1) {
cout << endl;
cout << "RULES: " << endl;
cout << "1) Choose your player." << endl;
cout << "2) Choose your army type." << endl;
cout << "3) Choose to attack." << endl;
cout << endl;
}
// if game is selected
else if (menuInput == 2) {
cout << endl;
cout << "START" << endl;
// first do while loop, continent choice
do {
cout << "1) North America" << endl;
cout << "2) South America" << endl;
cout << "3) Europe" << endl;
cout << "4) Africa" << endl;
cout << "5) Asia" << endl;
cout << "6) Australia" << endl;
cout << "7) Antartica" << endl;
cout << "Choose your player from the list: ";
cin >> continentInput;
cout << endl;
// invalid display if selection not in range
if (continentInput <= 0 ||continentInput > 7) {
cout << "INVALID" << endl;
cout << endl;
}
} while (continentInput <= 0 || continentInput > 8);
// second do while loop, army type choice
do {
cout << "1) Army (Ground type forces)" << endl;
cout << "2) Navy (Sea type forces)" << endl;
cout << "3) Air Force (Air type forces)" << endl;
cout << "Choose your army type from the list: ";
cin >> armyInput;
cout << endl;
if (armyInput <= 0 || armyInput > 3) {
cout << "INVALID" << endl;
}
} while (armyInput <= 0 || armyInput > 3);
// third do while loop, who to attack
}
else if (menuInput == 3) {
cout << endl;
cout << "GAME OVER" << endl;
}
else {
// display invlaid input if number choice is not in given range
cout << "INAVLID INPUT" << endl;
}
} while (menuInput != 3);
return 0;
}
如果您将用户输入更改为数字,则可以引用一个充满选项的数组。
military[x] = { "Army", "Navy" ...};
fromWhere[x] = { "North America", "South" ...};
toKill[x] = { "Whoever has oil" ... };
//get user input.
//toKill[get user input];
//kill with military[get user input];
//kill form fromWhere[get user input];
//do some attack logic
提醒你一下,你的问题相当模糊。你能说清楚你需要什么吗?似乎您希望散列值并进行快速查找以避免字符串比较,但您不需要这样做,因为您正在抓取int
作为用户输入。
相关文章:
- Visual Studio C++ 它只构建选项卡中显示的文件吗?
- 根据饮食限制显示餐厅选项
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- 重现 cpp 首选项的仿冒规则中显示的示例代码
- g 编译选项,带有-STD = C 14编译错误显示Werror = C 11 -Compat
- Qt-在选项卡小部件中显示表单
- 当我仅使用链接器选项 /LTCG时,为什么Visual Studio在Linker命令行中显示选项 /PGD
- QTabWidget 或 QTabBar 使用 C++ 在不同的选项卡中显示相同的 QWidget
- C++测验:使用矢量正确循环显示答案选项
- 为什么我的输出显示显示计算器中的每个选项
- Qt 4.8.5 配置.exe只显示选项
- 显示应用程序列表,如Win7中的alt选项卡
- 添加选项以显示百分比时出现生成文件问题
- Mac OSX 上的 Eclipse CDT 未在首选项集中显示调试菜单
- SelectionSort with bool-数组的第一个值显示为整个选项卡
- Qt QGraphicsView:在选项卡中显示QGraphicsView
- Visual Studio 2012 for windows 8未显示win32控制台应用程序选项
- 选项来显示从模板创建的类
- 不显示列表中选定的选项
- 当头文件被预处理时是否有一个预处理器选项显示?