Not declared in this scope

Hello everyone! I have a question regarding the programm below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
    if(2==2)
    {
        int a;
        a=1;
        cout<<a;
    }
    else
    {
        cout<<0;
    }
    cout<<a;
    return 0;
}

It gives me the error:
 
C:\Users\Andreutza\Desktop\divide_et_impera\main.cpp|15|error: 'a' was not declared in this scope

, and I'd like to know why ://
I mean, the statement in the condition is true in such way that it passes through the first branche in which a is declared. Why does the compiler see 'a' at line 15 as undeclared though? Thank you very much!
int a is available only from where it is declared at line 7 up to the closing brace at line 10.

If you wanted it to remain usable at line 15, move the declaration to just before the start of the if statement at line 5.


See Name Visibility
http://www.cplusplus.com/doc/tutorial/namespaces/
Last edited on
Topic archived. No new replies allowed.