Vector Problem Debug assertion failed line779

ok the error says debug assertion failed vector subscript out of range line 779. Im nw to trying to use vectors so this is probably a simple and stupid question but it compiled with no problem but breaks as soon as i run the Clist function. Any help and maybe a bit of guidance where i went wrong with the vector would be
appreciated .
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
using namespace std;
ifstream fIn;
ofstream fOut;
int menu();
void Clist ();
int menuOpt;

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':	
	Clist();	
	break;
case 'b': 
	cout << "test2" << endl;
	break;
case 'c':
	return 0;
	break;
case 'A':
	Clist();
	break;
case 'B': 
	cout << "test2" << endl;
	break;
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');
}



void Clist()
{	
	vector<string> triviaName;
	vector<string> fileName;
	int a, b, c;
	fIn.open("list.csv");
  
	while(!fIn.eof())
	{
	a = 1;
	getline( fIn, triviaName[a], ',' );
	getline( fIn, fileName[a]);
	a++;
		}
	for(b = 1; b < a; b++)
	{
		c = 1;
		cout << triviaName[c] << " || " << fileName[c] << endl;
		c++;
	}

	fIn.close();	
  cin.get();
  cin.get();
  system("cls");

}


Line 779 O.O? Where is it?



1
2
3
4
5
6
7
while(!fIn.eof())
	{
	a = 1;
	getline( fIn, triviaName[a], ',' );
	getline( fIn, fileName[a]);
	a++;
		}


In this code, you are assigning a = 1 at the beginning of the loop? Other thing is vectors start from 0 and you are directly accessing it's first element in
1
2
getline( fIn, triviaName[a], ',' );
	getline( fIn, fileName[a]); 

It should be from 0.

Hope this helps !
std::vectors don't grow automatically.
Use std::vector::resize() to... well, resize it, and std::vector::push_back() to add an element to the back of the vector.
Also note that vectors, like arrays, use zero-based indexing, so the first element is 0, the second is 1, and so on. But don't let this confuse you into passing 0 to resize(). That will make the vector of size 0; it won't make element 0 the last element of the vector.
Topic archived. No new replies allowed.