What is “ Variable ' i ' was not declared in scope ” in c++?

  • A+
Category:Languages

While practicing c++ code, I used the variable that was declared in the for loop.i want it to use it again in another for loop. But it showed me an error that the

variable i was not declared in scope

and I tried the same loop in Eclipse IDE it showed me

the symbol i was not resolved.

The sample code looks similar to this:

 #include<iostream>      using namespace std;      int main(){         for(int i=0;i<10;i++){             cout<<i;         }         for(i=10;i<20;i++){             cout<<i;         }     } 

 


You have to declare the variable for each scope:

#include<iostream>  using namespace std;  int main(){     for(int i=0;i<10;i++){         cout<<i;     }     for(int i=10;i<20;i++){         cout<<i;     } } 

After the first loop, there is no i anymore. You can try what the compiler says and see that this will fail:

int main(){     for(int i=0;i<10;i++){         cout<<i;     }     cout<<i; // Error } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: