【发布时间】:2021-03-16 22:35:00
【问题描述】:
我目前正在为我在大学的一门入门课程做一个期末项目,我似乎无法找出为什么我的输出会过早地中断。我正在尝试将同一目录中的文本文件的行附加到向量中,并从向量中随机选择以获取基于文本的格斗游戏中敌人的名称,但在测试期间,它似乎只是向量包含三个项目或最后一个 for 循环(注释块之前的那个,我正在分块处理)只显示三个可能的名称。代码如下:
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <ctype.h>
using namespace std;
int main() {
srand(time(0));
int userInput;
int player_health = 100;
int boss_health = 200;
int boss_index = rand() % 3;
ifstream boss_list;
string boss_name = "";
string boss_inputs[3] = {"MACHINE GUN", "BOMB ATTACK", "CANE SMACK"};
string user_inputs[3] = {"SWORD ATTACK", "BOMB ATTACK", "MAGIC ATTACK"};
vector<string> boss_list_holder = {};
unordered_map<string, int> attacks = {
{"SWORD ATTACK", rand() % 100},
{"BOMB ATTACK", rand() % 100},
{"MAGIC ATTACK", rand() % 100}
};
unordered_map<string, int> boss_attacks = {
{"MACHINE GUN", rand() % 75},
{"BOMB ATTACK", rand() % 80},
{"CANE SMACK", rand() % 50}
};
boss_list.open("names.txt", ios::in);
while(getline(boss_list, boss_name)) {
//cout << boss_name << endl;
boss_list_holder.push_back(boss_name);
}
for(int j=0; j<(sizeof(boss_list_holder)/sizeof(boss_list_holder[0])); j++) {
cout << boss_list_holder[j] << endl;
}
//cout << "BOSS FIGHT SIMULATOR\n\n";
/*
while(userInput > 3 || userInput < 1) {
cout << "Enter the attack you want to use!\n";
for(int i=0; i<sizeof(user_inputs)/sizeof(user_inputs[0]); i++) {
cout << i + 1 << ". " << user_inputs[i] << " ";
}
cout << "\n";
cin >> userInput;
};
*/
/*
cout << boss_inputs[boss_index] << " ";
cout << boss_attacks[boss_inputs[boss_index]];
*/
return 0;
}
我们将不胜感激。谢谢!
【问题讨论】: