variabel scope

I'm trying to create a random number generator, and need some for loops etc. But I have some problems with my variables.

Can someone please tell me why I get error
Foobar not declared in this scope
everytime I try to compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{

bool Bar = false;

....

for(int i=1; i<5; i++)
{
    int Foobar[5];

    Foobar[i] = Baz() %20 +1;    
    
    for(int x=1; x<i; x++) // Another for-loop inside the first one

        if(Foobar[i]==Foobar[x])    // Error: Foobar not declared in this scope
        Bar = true;

}

....

return 0;


Thanks in advance
It looks fine to me... What compiler are you using?

You may want to note, however that by declaring Foobar on line 10 a new one will be created each iteration of the loop on line 8. So the loop on line 14 will be checking uninitialized integers inside of the Foobar array except for the spot at i.
I'm using GNU GCC compiler and my IDE is Code::Blocks.

I tried to declare Foobar like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{

int Foobar[5];
bool Bar = false;

....

for(int i=1; i<5; i++)
{

    Foobar[i] = Baz() %20 +1;   // Error: Foobar is not declared in this scope

....


Now it will not work in my first for-loop...

But I guess I will have to leave my declaration outside the loop to not create a new one for each loop.
Maybe you are using the same name Foobar for a declaration of another object in the code above the loop and after the declaration of the array.
Last edited on
Topic archived. No new replies allowed.