Problem with two dimensional vector

I write a function, which fill vtwo dimensional vector like a matrix, and its work. Then i read function for output and there is the problem. When for index i input even number( i think that is the right word - mean "number who %2==0 i c++ language") the program output rightly and then close unexpectly. With odd numbers it works nice.
there is the code:
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
vector< vector<int> > t;
void fill(int num)
{int k=1;

	for(int i=0;i<num;i++)
	{vector<int> s;
		for(int j=0;j<num;j++)
		{s.insert(s.end(),k);k++;}

		t.insert(t.end(),s);}
}


void output()
{
	for(int i=0;i<t[i].size();i++)
	{ 

          for(int j=0;j<t[i].size();j++)
		cout<<t[i][j].n;
           cout<<endl;}

}

void main()
{int num;
 cout<<endl<<" Input number";
  cin>>num;
  fill(num);
  output();

}
Here's the problem: on line 16, you're checking that i is lower than t[i].size(). If i is at one moment >=t.size() (which will always happen at the ideal end of the loop), that comparison is going to throw an exception, because you're trying to access an element beyond the end of the vector. What you really want to do is check with t.size(), not t[i].size(). It was working with odd numbers probably because of the code your brain-dead compiler generated. MinGW generates code that crashes no matter what the input is.
You have another error on line 20: n is not a member of int. I have no idea how your compiler didn't complain about this.
Also, void main() is non-standard.
Last edited on
I fill the vectors like a square, this means that t.size() is equal to t[i].size(), becouse its square and each borders is equals. And i write it with even numbers it dont works but with odd numbers works.
I understood what you're doing perfectly.
For each valid t[i[b][/b]], t[i].size is indeed equal to t.size().
Let's assume that the user entered 10. that means that all valid t[i] are t[0..9]. It order to check after the last iteration, when i==10, whether to continue, you need to check that (i=10)<t[(i=10)].size(). Since the last valid t[i] is t[9], when you try to access t[10] to check its size, the vector class throws an exception and the program crashes.
The correct way is to always check the size of the vector you're currently traversing, even if you know that the inner vectors will be of the same size.

The fact that it works [i]for you[/i] with odd numbers is a coincidence. I compiled it and it crashed no matter what my input was.
Last edited on
Well i always be used the current size of the vector with i works.
But don't understand what means
"Also, void main() is non-standard."
Is has another way to write void main() ? Or must return this:
 
return;
It means you should always write main() as either of these two:
1
2
3
4
5
6
7
8
9
int main(){
    //...
    return 0;
}
//OR
int main(int argc,char **argv){
    //...
    return 0;
}

Any other variations, such as
1
2
3
4
5
main(){
}
//OR
void main(){
}

are illegal.
Last edited on
Is this can corrupt a problem, or this is a good style of writing programs. Becouse in school always we write main functions with void and anyone dont tell me that is wrong
It's always better to adhere to the standard. The fact that some compilers allow void main() doesn't mean that all do. If your teacher doesn't tell you anything about using void main(), then I don't think s/he's a very good teacher.
Thanks i remember it.
Last edited on
closed account (S6k9GNh0)
It's the CORRECT way to write a program in C++. Any other way is WRONG.
Topic archived. No new replies allowed.