Vector Problems

Ok my vectorz<player> pl works great but i think the rest are causing this error
Expresion Vector subscript out of range
it says its on line 779 but thats the end of the file I put bold on the problem spot for vectors and some explanation in load()
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <fstream>
using namespace std;
void load();
void playerinfo();
int textcolor;
int totalPlayers;
ifstream file;


class player{
public:
	int getdificulty()
	{
		return dificulty;
	}
	int getscore()
	{
		return score;
	}
	string getname()
	{
		return name;
	}
		int getcolor()
	{
		return color;
	}
	void Setdificulty(int input)
	{
		dificulty = input;
	}
	void Setname(string input)
	{
		name = input;
	}
	void Setscore(int input)
	{
		score = input;
	}
		void Setcolor(int input)
	{
		color = input;
	}
private:
	string name;
	int dificulty;
	int score;
	int color;
};
class game{
	public:
		string getquestion(){
			return question;
		}
		string geta(){
			return Awns_A;
		}
		string getb(){
			return Awns_B;
		}
		string getc(){
			return Awns_C;
		}
		string getcorrect(){
			return Awns_Correct;
		}
		void setquestion(string input){
			question = input;
		}
		void seta(string input){
			Awns_A = input;
		}
		void setb(string input){
			Awns_B = input;
		}
		void setc(string input){
			Awns_C = input;
		}
		void setcorrect(string input){
			Awns_Correct = input;
		}
	private:
			string question;// List of questions
			string Awns_A;// option A
			string Awns_B;// option B
			string Awns_C;// option C
			string Awns_Correct;// Correct Awnser
};
vector<player> Pl;
vector<game> Kids; //ok these are the vectors that are giving me problems
vector<game> Teens;// Check the load function to see how i am using them 
vector<game> Adults;
vector<game> Saints;

void main()
{

playerinfo();
load();
int i;
			for (i = 0; i < totalPlayers; i++)// Ask Each player the Question
			{
				cout << "	" << Pl[ i ].getname() << endl << endl;
			
				cout << Kids[ i ].getquestion() << endl;
				cout << endl
					 << "	Option A: " << Kids[ i ].geta() << endl
					 << "	Option B: " << Kids[ i ].getb() << endl
					 << "	Option C: " << Kids[ i ].getc() << endl << endl << "	";
			}
//for(i = 0; i <totalPlayers; i++)
//		cout << Pl[ i ].getname() << " " << Pl[ i ].getdificulty() << " " << Pl[ i ].getscore() << "\n";
		//cin >> i;
		}
void playerinfo()
{
	HANDLE  hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	int i;
	string input;
	int exp, color;
	cout << "How Many Players will Be Testing there Knowlege Today?\n";
	cin >> totalPlayers;
	for(i = 0; i <totalPlayers; i++)
    Pl.push_back(player());
	for(i = 0; i <totalPlayers; i++)
	{
		cout << "Player " << i+1 << " Please Enter Your Name\n";
		cin >> input;
		Pl[ i ].Setname(input);
		color = i + 2;
		Pl[ i ].Setcolor(color);
		textcolor = Pl[ i ].getcolor();
		SetConsoleTextAttribute(hConsole, textcolor);
		cout << Pl[ i ].getname();
		textcolor = 7;
		SetConsoleTextAttribute(hConsole, textcolor);
		cout << " Please Select Your Experience Level\n1.) Kids\n2.) Teenagers\n3.) Adults\n4.) Saints\n";
		cin >> exp;
		Pl[ i ].Setdificulty(exp);
		exp = 0;
		Pl[ i ].Setscore(exp);
	}
}

void load()
{
HANDLE  hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int i, dificulty;
for(i = 0; i <totalPlayers; i++){
	dificulty = Pl[ i ].getdificulty();
	cout << dificulty;
	string input;
	switch(dificulty)
	{
		case 1:
			file.open("kids.txt");
			while(!file.eof())// loop to end of file
			{
				for(i = 0; i <100; i++)
			Kids.push_back(game());/* i thought it was just a size issue so i put this in to increase the index of Vectorkids this is the only spot i tried it The rest of the code in case 1 just reads a file and tells the program where to put the information in vector<game> Kids the other case's Do the same thing for each of the other dificulty leves Teens, Adults, Saints*/
				
			getline( file, input, ',' );// get question
			Kids[ i ].setquestion(input);
			getline( file, input, ',' );//Get option A
			Kids[ i ].seta(input);
			getline( file, input, ',' );// Get option B
			Kids[ i ].setb(input);
			getline( file, input, ',' );//Get option C
			Kids[ i ].setc(input);// Store Option C
			getline( file, input);// Get Correct Option ie A,B,C
			Kids[ i ].setcorrect(input);
					}
            file.close();// Close file
			file.clear();// Clear file
			break;
			case 2:
			file.open("teens.txt");
			while(!file.eof())// loop to end of file
			{
			Teens.push_back(game());
			getline( file, input, ',' );// get question
			Teens[ i ].setquestion(input);
			getline( file, input, ',' );//Get option A
			Teens[ i ].seta(input);
			getline( file, input, ',' );// Get option B
			Teens[ i ].setb(input);
			getline( file, input, ',' );//Get option C
			Teens[ i ].setc(input);// Store Option C
			getline( file, input);// Get Correct Option ie A,B,C
			Teens[ i ].setcorrect(input);
					}
            file.close();// Close file
			file.clear();// Clear file
			break;
			case 3:
			file.open("adults.txt");
			while(!file.eof())// loop to end of file
			{
			Adults.push_back(game());
			getline( file, input, ',' );// get question
			Adults[ i ].setquestion(input);
			getline( file, input, ',' );//Get option A
			Adults[ i ].seta(input);
			getline( file, input, ',' );// Get option B
			Adults[ i ].setb(input);
			getline( file, input, ',' );//Get option C
			Adults[ i ].setc(input);// Store Option C
			getline( file, input);// Get Correct Option ie A,B,C
			Adults[ i ].setcorrect(input);
					}
                        file.close();// Close file
			file.clear();// Clear file
			break;
			case 4:
			file.open("saints.txt");
			while(!file.eof())// loop to end of file
			{
			Saints.push_back(game());
			getline( file, input, ',' );// get question
			Saints[ i ].setquestion(input);
			getline( file, input, ',' );//Get option A
			Saints[ i ].seta(input);
			getline( file, input, ',' );// Get option B
			Saints[ i ].setb(input);
			getline( file, input, ',' );//Get option C
			Saints[ i ].setc(input);// Store Option C
			getline( file, input);// Get Correct Option ie A,B,C
			Saints[ i ].setcorrect(input);
					}
            file.close();// Close file
			file.clear();// Clear file
			break;
			default:// Default Error Message
			system("cls");
			cout << "Error " << Pl[ i ].getname() << " did not select a valid dificulty level. Please Try again" << endl;
			cout << dificulty;
			cin.get();
			cin.get();
			system("cls");
			break;
	}
}
}
How do you know how large the arrays ought to be?

Kids is given an arbitrary size (100), but Teens, Adults, Saints aren't resized at all. Any access to one of these will give you a range error.
I appreciate the use of code tags but please take the extra effort of fixing the block alignment in the code that was posted. It is rather difficult to read.

It does seem strange that the kids is limited to 100 entries but the other cases loop until EOF is reached. By the way, those loops are not going to work correctly. Search the web for the terms "while eof" and you will find many threads on this. One of your getline operations will fail before the eof flag is set within the stream. You need to be checking the result of every getline call.

1
2
3
4
5
6
7
8
if(!getline( file, input))
{
   // failed!
}
else
{
   // worked!
}


I think that there are a few other design problems but you'll have to use the debugger to sort those out. It should be pretty easy to step through the program line by line and analyze the state of variables as you go. If you need more help, post an updated program with proper indenting so that it is more readable, post the contents of the input files, and tell us what steps you took to debug this on your own.
Topic archived. No new replies allowed.