Vector problem


ok the problem i have is in the game function at the bottom. the program is suppised to open a file parse it and then ask each player in the playerID vector each question. the problem is after the first question i get a assertion failed subscript out of range error. any ideas what the hell it might be.
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
		#include <iostream>
		#include <fstream>
		#include <string>
		#include <stdlib.h>
		#include <vector>
		using namespace std;
		ifstream fIn;
		ifstream Gamefile;
		ofstream fOut;
		int menu();
		int Clist ();
		int menuOpt;
		void game ();
		vector<string> fileName;
		int main()
		{
		menu();
		}
		int menu()
		{
		char inp;

		do
		{
		cout << "Please chose from the following options" <<endl;
		cout << "A: Load Trivia type." <<endl;
		cout << "B: Start Game." <<endl;
		cout << "C: Exit Game" <<endl;
		cin >> inp;
		string bt;	

		switch(inp)
		{
		case 'A': case 'a':	
			Clist();	
			break;
		case 'B': case 'b': 
			game();
			break;
		case 'C': case 'c':
			return 0;
			break;
		default:
			cout << "error that is not an option\n Press enter to continue " << endl;
			cin.get();
			cin.get();
			system("cls");
			break;
		}}while(inp != 'c');
		}



		int Clist()
		{	
			vector<string> triviaName;
			string input;
			int a, b, c, d;
			fIn.open("list.csv");
		    a = 0;
			while(!fIn.eof())
			{
			
			getline( fIn, input, ',' );
			triviaName.push_back(input);
			getline( fIn, input);
			fileName.push_back(input);
			a++;
			fileName.resize(a);
			triviaName.resize(a);
				}
			c = 0;
			for(b = 1; b < a; b++)
			{
				
				cout << "option# "<< c << ": " << triviaName[c] << endl;
				c++;
			}
			cout << "Please Enter your option: ";
			cin >> d;

			fIn.close();
			fIn.clear();
		  system("cls");
		  return d;

		}

//Problem Should be in this function.
           void game()
		{   vector<string> PlayerID;
			string input;
			int PlayerNumber, counter, PlayerCount, GamePlay, vectorSize, vectorCounter, a;
			
			cout << "	How many Players will be playing? ";
			cin >> counter;
			cout << endl;
			PlayerCount = 0;
			for (PlayerNumber = 1; PlayerNumber <= counter; PlayerNumber++)
			{
				system("cls");
				cout << "	Please enter name for player " << PlayerNumber << endl;
				cin >> input;
				PlayerID.push_back(input);
				PlayerCount++;
				PlayerID.resize(PlayerCount);
			}


			GamePlay = Clist();
			//decla ration for vectors that will parce the game csv file
			vector<string> question;
			vector<string> Awns_A;
			vector<string> Awns_B;
			vector<string> Awns_C;
			vector<string> Awns_Correct;
			Gamefile.open(fileName[GamePlay].c_str());

			vectorSize = 1;
			while(!Gamefile.eof())
			{
			
			getline( Gamefile, input, ',' );
			question.push_back(input);
			getline( Gamefile, input, ',' );
			Awns_A.push_back(input);
			getline( Gamefile, input, ',' );
			Awns_B.push_back(input);
			getline( Gamefile, input, ',' );
			Awns_C.push_back(input);
			getline( Gamefile, input);
			Awns_Correct.push_back(input);
			vectorSize++;
			question.resize(vectorSize);
			Awns_A.resize(vectorSize);
			Awns_B.resize(vectorSize);
			Awns_C.resize(vectorSize);
			Awns_Correct.resize(vectorSize);
				}
            Gamefile.close();
			Gamefile.clear();
			vectorCounter = 0;
/*this will need to be put in a nested loop to give each player a chance  respond to the question*/
			for (PlayerNumber = 0; PlayerNumber <= PlayerCount; PlayerNumber++)
			{
				cout << PlayerID[PlayerNumber] << endl
					 << question[vectorCounter] << endl
					 << "	Option A:" << Awns_A[vectorCounter] << endl
					 << "	Option B:" << Awns_B[vectorCounter] << endl
					 << "	Option C:" << Awns_C[vectorCounter] << endl;
				cin.get();
				cin.get();
				

				PlayerCount++;
				}
				
			
		}
What's the input file?

BTW, if you find yourself with code like:
1
2
3
4
5
vector<string> question;
vector<string> Awns_A;
vector<string> Awns_B;
vector<string> Awns_C;
vector<string> Awns_Correct;


you probably need a collection of records, rather than many collections of fields.
ok this is the list .csv file

Movie Trivia,movie.csv
Sports Trivia,sports.csv
Music Trivia,music.csv
Science Trivia,Science.csv


As you can see it just gives the trivia type and then the file the questions are stored in.

and this one is movie.csv just as an example

Who stared in Predator 1?,Andrew Jackson,Bob Weaver,Arnold Scwarzadinger,C
What Type of Movie was AppleSeed?,Action,Annimation,forien,B
Who Makes the Best Trivia About Movies?,JohnDoe,Bob Home,Me,C

this one is set
Question , Awns_A , Awns_B, Awns_C, AwnsCorrect
"you probably need a collection of records, rather than many collections of fields" Keep in mind im new to programming but what do you mean
A suitable record might look like:
1
2
3
4
5
6
7
8
struct QAEntry
{
    std::string question;
    std::string a;
    std::string b;
    std::string c;
    std::string correct;
};


You could then keep all the information together all the time.
 
typedef std::vector<QAEntry> QAEntries;


As for reading each line, I'd read the whole line, then parse the components, and move the whole mess out to a separate function.
1
2
3
4
5
6
7
8
9
void ReadEntry(QAEntries &items, const std::string &line)
{
    QAEntry item;
    // find ?
    // copy from beginning to ? into item.question
    // do the other fields ...

    items.push_back(item);
}


I don't want to write the whole thing for you, but I hope this helps.
I will have to try that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (PlayerNumber = 0; PlayerNumber <= PlayerCount; PlayerNumber++)
			{
				cout << PlayerID[PlayerNumber] << endl
					 << question[vectorCounter] << endl
					 << "	Option A:" << Awns_A[vectorCounter] << endl
					 << "	Option B:" << Awns_B[vectorCounter] << endl
					 << "	Option C:" << Awns_C[vectorCounter] << endl;
				cin.get();
				cin.get();
				

				PlayerCount++;// Found the problem child
				}
Topic archived. No new replies allowed.