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

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
}

What is Juneteenth?, On June 19, 1865, enslaved African-Americans in Galveston, Texas, were told they were free. Now, 155 years later, people in cities and towns  What does > mean? > is a symbol that means “greater than.” In math, it shows one value is larger than another (4 > 3). It can also conveniently stand in for the phrase greater than in casual writing (triceratops > T-rex, which it is, folks).

i only defined within the scope of the first for loop, so it needs to be re-declared in the second one.

Early Microsoft C++ compilers had a bug where the i leaked into the scope of the for loop to produce effectively

int i;
for (i = 0; i < 10; i++){

What is the evidence to support the 2-metre social distancing rule to , A parallel rapid review in this series, covering which clinical procedures are classed as aerosol generating, is due to report shortly. Maintaining  United Kingdom Climate Change Act: The United Kingdom Climate Change Act is the world's first legally binding environmental sustainability framework. Passed in November 2008, the initiative aims to lower greenhouse gas emissions by 80% by 2050 compared to the 1990 baseline. Under the Climate Change Act, the UK aims to become a low-carbon economy.

By writing for(int i=0; i<10; i++) {...} you declare the int i inside the for loop scope and it only has effect inside the for loop.

If you want to re-use the int i then you should place it outside of & before any for loop:

#include<iostream>

using namespace std;

int main(){
    int i = 0;
    for(i=0; i<10; i++){
        cout<<i;
    }
    for(i=10; i<20; i++){
        cout<<i;
    }
    cout<<i; // <- fine, 20
}

Or, simply repeat the declaration with each for loop then int i in for loops are totally different variables.

#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;
    }
    //cout<<i; <- oops!!! error
}

Symptoms of Coronavirus, What is social distancing? two people with masks on 6 feet apart. Limiting face-to​-face contact with others is the best way to reduce the spread  Similar to a pox party, adults expose themselves to people who have contracted Coronavirus (COVID-19) disease in the misguided hope that they will have recovered from it before hospitals have been over-run with people and treatment isn't possible.

Social Distancing, Quarantine, and Isolation, Twenty years after the introduction of the theory, we revisit what it does—and doesn't—explain. List of all math symbols and meaning - equality, inequality, parentheses, plus, minus, times, division, power, square root, percent, per mille,

What Is Disruptive Innovation?, These send information about how our site is used to services called Adobe Analytics, Hotjar you're worried about your symptoms; you're not sure what to do. The pound sign if you live in the United States. Although according to the Telephone Companies in the US (before AT&T had to be broken up), they wanted it to be called an OCTOTHORPE.

What is AmeriCorps?, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I?

Comments
  • You have to declare it again in the second loop, because it goes out of scope after the first loop.
  • i only exists inside the first loop, because you declared it there. Trying to use it in the second loop gives you the error. If you moved the int i; to before that loop, or redeclared it in the second loop, it would work.
  • works for me. I'm using Visual Studio 6 ;)
  • @virgesmith: Yes, that's a bug in that compiler.
  • @Bathsheba, I know. I havent actually used VS6 for donkeys years but the pain of fixing all that non-compliant stuff is still fresh.
  • do you mean i leakes out of the scope of the for loop? Or was the bug actually about i of one loop leaking into the scope of the next loop?
  • @user463035818: I've put the equivalent code in. As far as I can remember the initialisation of i took place in the loop itslef.
  • interesting, guess that can cause all sorts of confusing bugs, on the other hand there should be an error if i was already declared in the outer scope