Unhandled exception error

Hi!
This is all my code. When i debug it, at the 15th line ( c[i].push_back(card[j]); ) i get
"Unhandled exception in Cpp.exe: 0xC0000005: Access Violation." error. Please help me to solve it.
P.S. Cpp.exe is the name of my code , i use C++ 6.0 and sorry for my english :D

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 <vector>
using namespace std;
vector< vector<int> > c;
int main()
{
	vector<int> card;
	int n,k,p,i,j;
	cin>>n>>k;
	for(i=1 ; i<=k ; i++)
		card.push_back(i);
	cin>>p; i=1; j=0;
	while(!card.empty())
	{
		c[i].push_back(card[j]);
		card.erase(card.begin()+j);
		j = j==card.back() ? 1 : j+p-1;
		i = i==n ? 1 : i+1;
	}
	for(i=0 ; i<c[n].size() ; i++)
		cout<<c[n][i]<<endl;
	return 0;
}
Last edited on
c[i].push_back(card[j]);
You're accessing memory you don't have. You never used push_back before this, so the vector "c" (name your variables more useful names) is empty, so you can't use c[i].

It's a vector of vectors of type int. You need to push_back a vector of int.

c.push_back(card);
Now after this, you can do c[0]

Another problem is that for some reason, you set "i" equal to 1? Why? Even if you do the push_back thing I just suggested, c[i] would still not work because i = 1 and you only got access to c[0].
Thank you very much i understand my mistake , but how can i get acces to c[1] , c[2]... ?
If you want to access c[1], c[2] etc. Then you need to push_back more than one vector of integer.
OK ) Thanks very much .
Topic archived. No new replies allowed.