C++ 如何从不同的行获取存储在类中的变量?

C++ 如何从不同的行获取存储在类中的变量?,c++,class,C++,Class,如何让程序读取.txt文件中的每一行,并将每一行的3个变量存储在不同的位置?我不明白如何在同一个类中存储不同的值。一句话很好,但我试过的更多的话不起作用 class Team { public: string name; string dificulty; string section; }; void GetTeamInfo(Team& ko); int main() { Team ko; GetTeamInfo(ko);

如何让程序读取.txt文件中的每一行,并将每一行的3个变量存储在不同的位置?我不明白如何在同一个类中存储不同的值。一句话很好,但我试过的更多的话不起作用

class Team
{
    public:
    string name;
    string dificulty;
    string section;
};

void GetTeamInfo(Team& ko);

int main()
{   
    Team ko;
    GetTeamInfo(ko);
    cout << ko.name << " ";
    cout << ko.dificulty<< " ";
    cout << ko.section<< " "; 

    system("PAUSE");
}

void GetTeamInfo(Team& ko, int & i)
{
    ifstream fd;
    fd.open("Team.txt");
    if (fd.is_open())
    {
        for(int i = 0; i < 10 ; i ++)
        {
        fd >> ko.name;
        fd >> ko.dificulty;
        fd >> ko.section ;

        }

    }
    else
    {
        std::cout << "Mistake can't open file 'Team.txt'\n";
    }
}
班级团队
{
公众:
字符串名;
串困难;
弦段;
};
void GetTeamInfo(团队和ko);
int main()
{   
高队,;
GetTeamInfo(ko);
cout-ko.section;
}
}
其他的
{
std::cout试试这个:

void GetTeamInfo(vector<Team>& kos)
{
    ifstream fd;
    fd.open("Team.txt");
    if (fd.is_open())
    {
        while (!d.eof())
        {
            Team ko;
            fd >> ko.name;
            fd >> ko.dificulty;
            fd >> ko.section;
            kos.push_back(ko);
        }
    }
    ...
}
void GetTeamInfo(向量和kos)
{
iffd;
fd.open(“Team.txt”);
如果(fd.is_open())
{
而(!d.eof())
{
高队,;
fd>>ko.name;
fd>>ko.困难;
fd>>ko.section;
kos.推回(ko);
}
}
...
}
我建议您使用a,因为您有许多团队

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

class Team
{
    public:
    string name;
    string dificulty;
    string section;
};

void GetTeamInfo(vector<Team>& ko_v);

int main()
{
    vector<Team> ko; // a vector of Teams
    GetTeamInfo(ko); // read from file inside a vector

    // print every team
    for(unsigned int i = 0; i < ko.size(); ++i) {
      cout << ko[i].name << " ";
      cout << ko[i].dificulty<< " ";
      cout << ko[i].section<< " ";
      cout << "\n";
    }


    //system("PAUSE"); // don't use system()
    return 0; // return 0 should be placed at the end of main
}

void GetTeamInfo(vector<Team>& ko_v) // you had an extra parameter here, no need to
{
    ifstream fd;
    fd.open("Team.txt");

    if (fd.is_open()) // check if file is open
    {
        while (!fd.eof()) // while you have more to read
        {
            Team ko;            // create a Team
            fd >> ko.name;      // read data
            fd >> ko.dificulty;
            fd >> ko.section;
            ko_v.push_back(ko); // store that Team in the vector of teams
        }
    }
    else
    {
      cout << "File not opened!\n";
    }
}

使用向量,这里有一个完整的示例(注释):

#包括
#包括
#包括
#包括
使用名称空间std;
班队
{
公众:
//添加构造函数。
团队(字符串名称、字符串难度、字符串部分):
姓名,
困难(困难),
部门(部门)
{}
字符串名;
串困难;
弦段;
};
//定义便利类型;
typedef矢量团队列表;
//此函数现在接收团队向量。
void GetTeamInfo(团队列表和tl);
int main()
{   
团队名单;
GetTeamInfo(tl);
for(vector::iterator it=tl.begin();it!=tl.end();++it)

这里不能说出如何使用数组?如果有3个以上的团队怎么办?比如一次10个团队,另一次25个?随机。
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class Team
{
    public:
    string name;
    string dificulty;
    string section;
};

void GetTeamInfo(Team* ko_ar, const int N);

int main()
{
    const int N = 3; // number of teams in the file
    Team ko[N]; // a vector of Teams
    GetTeamInfo(ko, N); // read from file inside a vector

    // print every team
    for(int i = 0; i < N; ++i) {
      cout << ko[i].name << " ";
      cout << ko[i].dificulty<< " ";
      cout << ko[i].section<< " ";
      cout << "\n";
    }


    //system("PAUSE"); // don't use system()
    return 0; // return 0 should be placed at the end of main
}

void GetTeamInfo(Team* ko_ar, const int N)
{
    ifstream fd;
    fd.open("Team.txt");

    int i = 0;
    if (fd.is_open()) // check if file is open
    {
        while (!fd.eof()) // while you have more to read
        {
            Team ko;            // create a Team
            fd >> ko.name;      // read data
            fd >> ko.dificulty;
            fd >> ko.section;
            if(i == N) {
              cout << "Read more than " << N << " teams\n";
              break;
            }
            ko_ar[i++] = ko; // store that Team in the vector of teams
        }
    }
    else
    {
      cout << "File not opened!\n";
    }
    cout << "Read " << i << " teams\n";
}
#include <vector>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Team
{
    public:
        // Adding a constructor.
        Team(string name, string dificulty, string section):
        name(name),
        dificulty(dificulty),
        section(section)
        {}

        string name;
        string dificulty;
        string section;
};

// Defining a convenience type;
typedef vector<Team> team_list_t;

// This function now receives a vector of teams.
void GetTeamInfo(team_list_t &tl);



int main()
{   
    team_list_t tl;
    GetTeamInfo(tl);

    for (vector<Team>::iterator it = tl.begin(); it != tl.end(); ++it)
        cout << it->name << " " << it->dificulty << " " << it->section << endl;

    // You can also ...
    for (int i = 0; i < tl.size(); i++)
        cout << tl[i].name << " " << tl[i].dificulty << " " << tl[i].section << endl;
}


void GetTeamInfo(team_list_t& tl)
{
    ifstream fd;
    fd.open("Team.txt");
    if (fd.is_open())
    {
        // Define variables;
        string name, dificulty, section; 

        // Read until EOF
        while(fd >> name >> dificulty >> section)
        {
            // Add teams to the vector/list.
            tl.push_back(Team(name, dificulty, section));
        }
    }
    else
    {
        std::cout << "Mistake can't open file 'Team.txt'\n";
    }
}