当用户键入一个单词时,程序正在搜索数组中的匹配项,如果匹配,程序将打印出数组中的字符串。下面的代码就是这样。我的目标是当一个单词与数组中的单词匹配时,程序应该打印一个信息,而不仅仅是单词。我以为我可以用函数填充数组,但它不起作用。它甚至可能吗?
我正在使用传奇英雄名字的联盟,因为它们很多,而且我知道它们并且它会让我花费大量时间来考虑名字:D
这个想法是,如果用户输入voly,程序会在数组中找到voly并打印出(例如)他的起始生命,盔甲,先生等等。
我尝试了许多功能,但是我无法使其发挥作用。
#include <iostream>
#include <string>
using namespace std;
string voly(string holder,string heroName);
int main(){
const int numH = 10;
string holder;
string heroName;
string heroList[numH] = {"voly", "teemo", "vladimir", "morgana", "jax", "ekko", "anivia", "nunu", "ashe", "tresh" };
cout << "Enter hero name.\n" << endl;
cin >> heroName;
for (int i = 0; i < numH; i++){
holder = heroList[i];
if (heroName == holder){
cout << holder << endl;
}
}
system("PAUSE");
return 0;
}
string voly(string holder, string heroName) {
cout << "Voly is the best" << endl;
}
答案 0 :(得分:0)
尝试了解结构。您可以利用它们将所有英雄信息封装在英雄结构中,如下所示。这可以作为每个英雄的原型:
struct hero {
string name;
int hp;
int mana;
float mreg;
...
void printMe() { cout << 'hp: ' << hp << endl << 'mana: ' << mana << endl << ...; }
}
使用该特定英雄对象的printMe()函数,您可以打印其值。
然后,为每个英雄创建一个struct对象并将它们添加到数组中。
hero* pointer = new hero[numH];
pointer[0] = new hero {
name: "voly",
hp: 150
};
pointer[1] = new hero {
...
};
(尝试通过.CSV文件考虑一些导入功能。)
使用for循环,比较名称:
for (int i = 0; i < numH; i++){
if (heroName == pointer[i].name){
pointer[i]->printMe();
}
}
尝试使用Google查找相关教程。不幸的是,我不太确定C ++中的语法,也许有人可以帮助我。
祝你好运!