很抱歉发布带有代码的文本墙,但我似乎无法弄清楚为什么结构向量不能获取值。
void load() {
list.clear();
vector<string> tag;
vector<int> points;
ifstream scorelist, namelist;
scorelist.open("score.txt");
if (scorelist.is_open()) {
int scores;
while (scorelist.good()) {
cin >> scores;
points.push_back(scores);
}
scorelist.close();
}
namelist.open("name.txt");
if (namelist.is_open()) {
string text;
while (namelist.good()) {
getline(namelist, text);
tag.push_back(text);
}
namelist.close();
}
players games; //Players a struct with elements string name, int score
for (int i = 0; i < 10; i++) {
games.score = points[i];
games.name = tag[i];
list.push_back(games); //list is a vector<players>
}
}
例如,如果我在这里写cout << list[0].name
没有任何反应,则txt&#39; s具有值。
答案 0 :(得分:1)
您正在从cin
而不是scorelist
信息流中读取分数。变化:
cin >> scores;
要:
scorelist >> scores;