Adding items from a file to a vector of class

Hello. I need assistance on how to add a list of information from a file to a vector of a class. Here is my 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
#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
//the vector champions made in main
/*
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
#include <iostream>
#include "Champion_Info.h"

using namespace std;

int main()
{
    char answer;
    unsigned int i;
    Champ_Info champion;
    vector<Champ_Info> champions;
    do
    {
        //clears cin
        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();
    }
    return 0;
}
On line 103, don't use .fail(), use .is_open()

I'm not really sure what you're trying to do. What do you want to be in each string?
I'm trying to read in from a file a list of info. Like a name (champName) followed by a list of items (vector<std::string> items). I am then trying to input this one object (Champ_Info champion) into a vector of objects (vector<Champ_Info> champions)
Yes, but what should be in each string? It looks like you're trying to do something weird with delimiters.
Here's what an example of the txt file would look like:

name1: item1, item2, item3, item4, item5, item6.
name2: item1, item2, item3, item4, item5, item6.
.
.
.

This is what I'm trying to put within my vector class so I would have a list of them to use later in the program.
Will there always be 6 items or will the number of items vary?
For now it will always be 6 items, but I may change it to vary in the future.
Topic archived. No new replies allowed.