i cant understand???!!!

hi every body
i am trying to copy one array to a nother but i cant understand the errors that the compiler give it to me ??/




# include<iostream>
# include<string>

using namespace std;
int main()
{
int row,col;
int y[3][3];
int x[3][3]={
{1,2,3},
{4,5,6},
{7,8,6}
};
for(int i=0;i<=2;i++)
{ for(int j=0; j<=2;j++)
y[i][j]=x[i][j];
row=i;
col=j;
cout<<y[i][j]<<endl;
}cout<<x[i][j]<<endl;



the errors


Error 1 error C2065: 'j' : undeclared identifier
Error 2 error C2065: 'i' : undeclared identifier

and thanks any way



i and j are only visible in the scope in which they're declared. So i is visible in both loops and j is only visible in the inner loop.

I'm reposting your code with indentation. It should be clear that way.
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
#include<iostream>
#include<string>

using namespace std;

int main()
{
	int row,col;
	int y[3][3];
	int x[3][3]=
	{
		{1,2,3},
		{4,5,6},
		{7,8,6}
	};

	for (int i = 0; i <= 2; i++)
	{
		for (int j = 0; j <= 2; j++)
			y[i][j]=x[i][j];

		row=i;
		col=j;
		cout<<y[i][j]<<endl;
	}

	cout<<x[i][j]<<endl;	// i and j are unknown here
}
I'd like to point out that even if i and j existed at that position, accessing that element of the matrix would cause a buffer overflow.
That's right, their left being set to 3.

I also missed that j is unknown on line 24 too.
Suggestions for change are commented with "//"
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
include<iostream>
#include<string>//1. not required

using namespace std;

int main()
{
	int row,col;
	int y[3][3];
	int x[3][3]=
	{
		{1,2,3},
		{4,5,6},
		{7,8,6}
	};

	for (int i = 0; i <= 2; i++)//2. revise condition to i<3
	{
		for (int j = 0; j <= 2; j++)//3. ditto
			y[i][j]=x[i][j];

		row=i;//4. comment out
		col=j;//5. ditto
		cout<<y[i][j]<<endl;
	}
                for (int i = 0; i <= 2; i++)//6. revise condition to i<3 and add these 2 loops
	{
		for (int j = 0; j <= 2; j++)//7.ditto
	cout<<x[i][j]<<endl;	// i and j are unknown here-//8.  now they are 
}

Take a look at memcpy(p1,p2).....copies a block of memory (array) to another block.
Last edited on
Topic archived. No new replies allowed.