Adding data from a file into a class variable

Hello everyone. I am trying to add data from a file that would go into a class that would later go into a vector of a class. I'm not really sure how to do it exactly. Here is the code:

Champion_Info.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef CHAMPION_INFO_H_INCLUDED
#define CHAMPION_INFO_H_INCLUDED

#include <vector>
#include <string>

using namespace std;

class Champ_Info
{
  private:
      string champName;
      vector<std::string> items;
      /*
      string item1;
      string item2;
      string item3;
      string item4;
      string item5;
      string item6;
      */

  public:
      Champ_Info();
      void getName();
      void getItems();
      //prints out the champion name and its items
      void print();
      //sends the data to file
      void file();
      //gets info from a file
      void input();
      void Sorted();
      void Edit();
      ~Champ_Info();
};

#endif // CHAMPION_INFO_H_INCLUDED


Champion_Info.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "Champion_Info.h"

using namespace std;

Champ_Info::Champ_Info()
{

}

void Champ_Info::getName()
{
    cout << "Enter champion name: " << endl;
    getline(cin, champName);
}

void Champ_Info::getItems()
{
    string item_name;
    cout << "Enter name for:" << endl;

    cout << "first item: " << endl;
    //getline(cin, item1);
    getline(cin, item_name);
    items.push_back(item_name);

    cout << "second item: " << endl;
    //getline(cin, item2);
    getline(cin, item_name);
    items.push_back(item_name);

    cout << "third item: " << endl;
    //getline(cin, item3);
    getline(cin, item_name);
    items.push_back(item_name);

    cout << "fourth item: " << endl;
    //getline(cin, item4);
    getline(cin, item_name);
    items.push_back(item_name);

    cout << "fifth item: " << endl;
    //getline(cin, item5);
    getline(cin, item_name);
    items.push_back(item_name);

    cout << "sixth item: " << endl;
    //getline(cin, item6);
    getline(cin, item_name);
    items.push_back(item_name);
}

void Champ_Info::print()
{
    cout << champName << ": ";//<< item1 << ", " << item2 << ", " << item3 << ", " << item4 << ", " << item5 << ", " << item6 << "." << endl;
    for(int i = 0; i < 6; i++)
    {
        if(i == 5)
        {
            cout << items[5] << "." << endl;
        }
        else
        {
            cout << items[i] << ", ";
        }
    }
}

void Champ_Info::file()
{
    fstream fout;
    fout.open("Lol.txt", fstream::out | fstream::app);
    if(fout.fail())
    {
        cout << "Error, could not open" << endl;
    }

    fout << champName << ": ";//<< item1 << ", " << item2 << ", " << item3 << ", " << item4 << ", " << item5 << ", " << item6 << "." << endl;

    for(int i = 0; i < 6; i++)
    {
        if(i == 5)
        {
            fout << items[5] << "." << endl;
        }
        else
        {
            fout << items[i] << ", ";
        }
    }
}
//Don't know how to input info from file to my champion class made in main.
//not sure how to get it into champion class to push_back into the vector champions
//to get ready for the next set of data.
/*
void input()
{
    string temp;
    ifstream fin;
    fin.open("Lol.txt");
    if(fin.fail())
    {
        cout << "Error, file could not open or does not exist" << endl;
    }
    while(fin.good())
    {
        getline(fin, temp, ':');
        champName = temp;
        getline(fin, temp, ',');
        items.push_back(temp);
        getline(fin, temp, ',');
        items.push_back(temp);
        getline(fin, temp, ',');
        items.push_back(temp);
        getline(fin, temp, ',');
        items.push_back(temp);
        getline(fin, temp, ',');
        items.push_back(temp);
        getline(fin, temp, '.');
        items.push_back(temp);
    }
}
*/
//to be implimented later
void Sort()
{

}
//to be implimented later
void Edit()
{

}

Champ_Info::~Champ_Info()
{

}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include "Champion_Info.h"

using namespace std;

int main()
{
    //old code that was commented out for the time being
    /*
    char answer;
    unsigned int i;
    Champ_Info champion;
    vector<Champ_Info> champions;
    do
    {
        cin.clear();
        fflush(stdin);
        champion = Champ_Info();
        champion.getName();
        champion.getItems();
        champion.print();
        champions.push_back(champion);
        //champion.file();
        cout << "Do another?" << endl;
        cin >> answer;
    }
    while(answer == 'y' || answer == 'Y');
    for(i = 0; i < champions.size(); i++)
    {
        champions[i].print();
        champions[i].file();
    }
    */

    int action;
    char answer;
    unsigned int i;
    Champ_Info champion;
    vector<Champ_Info> champions;

    cout << "Choose an action: " << endl;
    cin >> action;
    while(action != 0)
    {
        switch (action)
        {
            case 1:
                do
                {
                    cin.clear();
                    fflush(stdin);
                    champion = Champ_Info();
                    champion.getName();
                    champion.getItems();
                    champion.print();
                    champions.push_back(champion);
                    cout << "Do another?" << endl;
                    cin >> answer;
                }
                while(answer == 'y' || answer == 'Y');
                break;

            case 2:
                for(i = 0; i < champions.size(); i++)
                {
                    champions[i].print();
                    champions[i].file();
                }
                break;

            case 3:
                //not sure if this is the proper way of reading from the file
                //not sure how to add the data to the class based on the functions I have
                /*
                string temp;
                ifstream fin;
                fin.open("Lol.txt");
                if(fin.fail())
                {
                    cout << "Error, file could not open or does not exist" << endl;
                }
                while(fin.good())
                {
                    getline(fin, temp, ':');
                    champName = temp;
                    getline(fin, temp, ',');
                    items.push_back(temp);
                    getline(fin, temp, ',');
                    items.push_back(temp);
                    getline(fin, temp, ',');
                    items.push_back(temp);
                    getline(fin, temp, ',');
                    items.push_back(temp);
                    getline(fin, temp, ',');
                    items.push_back(temp);
                    getline(fin, temp, '.');
                    items.push_back(temp);

                }
                */
                break;

            default:
                cout << "Try again" << endl;
                cin >> action;
                break;
        }
        cout << "Choose an action: " << endl;
        cin >> action;
    }

    return 0;
}


Thanks in advance for any help.
*bump*
What is the problem you are having? Is it reading data from a file? Is it storing that data in class members? Is it storing objects in a vector?

We're not mind-readers. You have to be clear about what it is that you want help with.
Topic archived. No new replies allowed.