0

It's saying on line 13: name lookup of 'i' changed for ISO 'for' scoping

#include <iostream>
using namespace std;

int main()
{
    int Fib[40];
    Fib[0] = 1;
    Fib[1] = 1;
    cout << Fib[0] << Fib[1] << endl;

    for (int i=2; i<40; i++)
    Fib[i]=Fib[i-1]+Fib[i-2];
        cout << Fib[i] << endl;
}
2
  • 2
    For loops only last for one line if there are no braces. Commented Dec 8, 2012 at 4:18
  • 1
    You're aware that because you didn't use curly braces only the first statement is inside the loop yes? the cout << Fib[i] << endl; line is outside the for loop. Also fix your indentation. Commented Dec 8, 2012 at 4:19

2 Answers 2

2

You forgot to scope the instructions you want to be executed in the loop:

for (int i=2; i<40; i++) {
    Fib[i]=Fib[i-1]+Fib[i-2];
    cout << Fib[i] << endl;
}

Loops will only execute a single instruction or a single scope of instructions. In your code, only the first line was part of the loop. The second was not, and therefore i was out of scope. Whether you indent the lines or not doesn't matter at all in C++. The compiler doesn't care about indentation; it is used only to aid the programmer, not the compiler.

Sign up to request clarification or add additional context in comments.

Comments

1

The last line has [i] it is not inside the loop cout << Fib[i] << endl; the scope of i is only within the for loop as i is declared within the loop according to new ISO standard so the correct line would be

for (int i=2; i<40; i++) {
    Fib[i]=Fib[i-1]+Fib[i-2];
    cout << Fib[i] << endl;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.