Why does my programe crashs when i use the Or statement ||

can u help me out with this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  if(x[1][j]!=x[2][j] || x[3][j]!=x[4][j] || x[5][j]!=x[6][j] || x[7][j]!=x[8][j] || x[9][j]!=x[10][j] || x[11][j]!=x[12][j])
		{
			break;
		}
		else
		{
			if(i != 11)
			{
				cout<<x[i][j]<<" ";
			}
			else
			{
				cout<<x[i][j]<<endl;
			}
		}
What does this code do?
it filter out some numbers from the nested vector i created
Can you show us your whole code?
Consider this:
1
2
3
4
5
char ch{};
do {
    cout << "continue? ";
    cin >> ch;
} while( ch != 'n' || ch != 'N' );

If I enter 'n',
ch != 'n' -> false
ch != 'N' -> true

If I enter 'N',
ch != 'n' -> true
ch != 'N' -> technically false, as it doesn't get evaluated

As you can see, the logical or operator(||) requires at least one expression to be true, so no matter what I input, the loop will keep running.

I assume it's the same issue with your code.


It might be that you are accessing outside your vector range.
Last edited on
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
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;



vector<int> Fill(int Ze)
{
	vector<int> colume;
	int IT=0;
	for(int i =0;i<4096;i++)
	{
		if (IT>=Ze)
		{
			colume.push_back(1);
		}
		else
		{
			colume.push_back(0);
		}
		IT++;
		if(IT==2*Ze)
		{
			IT=0;
		}
	}
	return colume;
}

int main()
{
	vector< vector<int> > x;
	for(int i=0;i<12;i++)
	{
		x.push_back(Fill(int(pow(2,i))));
	}

	for(int j =0;j<4096;j++)
	{	
		for(int i=0;i<12;i++)
		{
			if(i != 11 )
			{
				cout<<x[i][j]<<" ";
			}
			else
			{
				cout<<x[i][j]<<endl;
			}
		
		}
	}
}
when i add that part of condition it crashes
The problem is this one :
if(x[1][j]!=x[2][j] || x[3][j]!=x[4][j] || x[5][j]!=x[6][j] || x[7][j]!=x[8][j] || x[9][j]!=x[10][j] || [x[11][j] != x[12][j])
It might be that you are accessing outside your vector range. 


1
2
3
4
	for(int i=0;i<12;i++)
	{
		x.push_back(Fill(int(pow(2,i))));
	}

 
if(x[1][j]!=x[2][j] || x[3][j]!=x[4][j] || x[5][j]!=x[6][j] || x[7][j]!=x[8][j] || x[9][j]!=x[10][j] || x[11][j]!=x[12][j])


You ARE accessing outside your vector range! integralfx is right.

Index of x goes from 0 to 11, not 1 to 12.

There must be a better way of coding that logical statement, though - consider another loop.
Last edited on
<3 ya man ... thanx
Topic archived. No new replies allowed.