此读取功能有什么问题

What is wrong with this read function?

本文关键字:什么 问题 功能 读取      更新时间:2023-10-16

对不起,为用代码发布了文本墙,但我似乎无法弄清楚为什么结构向量没有得到值。

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的值都没有发生。

您正在阅读cin而不是从scorelist流中读取分数。更改:

cin >> scores;

to:

scorelist >> scores;