vector of class

Hi, I am making a simple program that is suppose to make a list of champions and their items from the game League of Legends. I am stuck on making a vector of the class so each slot within the vector would hold each champion and its data. This is what I got:

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
#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: ";
    getline(cin, champName);
}

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

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

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

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

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

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

    cout << "sixth item: ";
    //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] << ", ";
        }
    }
}

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(!eof())
    {
        getline(infile, temp, ':');
        champName = temp;
        getline(infile, temp, ',');
        items.push_back(temp);
        getline(infile, temp, ',');
        items.push_back(temp);
        getline(infile, temp, ',');
        items.push_back(temp);
        getline(infile, temp, ',');
        items.push_back(temp);
        getline(infile, temp, ',');
        items.push_back(temp);
        getline(infile, 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
#include <iostream>
#include "Champion_Info.h"

using namespace std;

int main()
{
    char answer;
    Champ_Info champion;
    do
    {
        champion = Champ_Info();
        champion.getName();
        champion.getItems();
        champion.print();
        champion.file();
        cout << "Do another?" << endl;
        cin >> answer;
    }
    while(answer == 'y' || answer == 'Y');
    return 0;
}


Any help with making this vector of the class would be appreciated. Thank you.
Somewhere at the beginning of main: std::vector<Champ_info> champions;
Between lines 16 and 17: champions.push_back(champion);
Ok. Thank you. =)
Ok, now I don't know how to input from a file into this vector I made. 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
#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
34
#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;
}
Last edited on
Topic archived. No new replies allowed.