Getting a Minor Error! Help!

So i have written this for a school assignment. I am doing everything fine except I get this error.

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
       
void fill(int data[8][8]) {
srand(time(0));
for (int x=0; x<8; x++){
for (int y=0; y<8; y++)
data[x][y] =(rand()%10);
}
}

void data(int data[8][8]){
for (int x=0; x<8; x++){ 
for (int y=0; y<8; y++){ 
cout<<data[x][y];
if (y<7) cout<<"  ";
}
cout<<endl;
}
}
 
void row(int data[8][8]){
for (int x=0; x<8; x++){
int total=0;
for (int y=0; y<8; y++) total += data[x][y];
cout<<"The sum of row "<<x+1<<" is "<<total<<"."<<endl;
}
}
void column(int data[8][8]){
       for (int x=0; x<8; x++){
       int total=0;
               for (int y=0; y<8; y++) total += data[x][y];
       cout<<"The sum of column "<< y+1 <<" is "<<total<<"."<<endl;
}
}


void diagonal(int data[8][8]){
int total=0;
int sum=0;
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
if (i==j) total += data[i][j];
if ((i+j)==7) sum += data[i][j];
}
}
cout<<"The sum of the diagonal "<<total<<"."<<endl;
cout<<"The sum of the diagonal "<<sum<<"."<<endl;
}

int main(){
int a[8][8];
fill(a);
data(a);
row(a);
column(a);
diagonal(a);
return 0;
}
               


This is the error i get.
sija8.cpp: In function âvoid column(int (*)[8])â:
sija8.cpp:35: error: name lookup of âyâ changed for ISO âforâ scoping
sija8.cpp:35: note: (if you use â-fpermissiveâ G++ will accept your code)

Now i put semicolon after the second for loop to fix it, but that messes up the program and messes up the order it needs to be in.
Your variable y is not defined in that scope. Remember that your for loop on line 34 ends in the same line and any variable you declare in there is only available in that loop.
Ahh there you go! thanks i had forgotten that was supposed to b x not y haha thanks
Here I modified the indentation to aid legibility.
1
2
3
4
5
6
7
8
9
10
void column(int data[8][8])
{
    for (int x=0; x<8; x++)
    {
        int total=0;
        for (int y=0; y<8; y++) 
            total += data[x][y];
        cout<<"The sum of column "<< y+1 <<" is "<<total<<"."<<endl;
    }
}

At line 6 variable int y is declared inside the for loop. It exists only within the scope of that loop, that is until the end of line 7.

Then on line 8 an attempt is made to use that variable y, but it no longer exists, as it is now out of scope.

The proper solution depends upon what the is intention, what is the code supposed to achieve?

Possibly this might be correct:
1
2
3
4
5
6
7
8
9
10
void column(int data[8][8])
{
    for (int y=0; y<8; y++) 
    {
        int total=0;
        for (int x=0; x<8; x++)
            total += data[x][y];
        cout<<"The sum of column "<< y+1 <<" is "<<total<<"."<<endl;
    }
}
Topic archived. No new replies allowed.