0

How to get program read every line from .txt file and store 3 variables per line in different place ? I don't understand how can I store different value in same class. One line works fine but what I have tried more doesn't work.

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";
    }
}
Niall
  • 30,036
  • 10
  • 99
  • 142
Blank
  • 43
  • 4

3 Answers3

1

Try this:

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);
        }
    }
    ...
}
barak manos
  • 29,648
  • 10
  • 62
  • 114
0

I suggest you use a std::vector, since you have a number of teams.

#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";
    }
}

Why not to use an array? You could use an array of course, but since this is C++, std::vector's usage is encouraged. Moreover, you don't have to worry about the number of the Teams you are going to read from the file. If you would have used an array, you should know apriori the number of the Teams, or dynamically allocate memory.

Why not use system(pause); ?

Just for a glance, I am modifying the example with the use of an array.

#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";
}
Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

Use a vector, here you have a full example(commented):

#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";
    }
}
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60