1

I have this c++ code for my uni project...

for (int a=0; a<definedgroups; a++)
{
    cout << "Enter Lower number for group " << a << ": "; 
    cin >> User_Groups [a] [0] ;

}

Now I want to declare another loop that displays the number

could I use A from the first variable and say...

while (a>0)
{
displays code, have written yet
}

or is that "a" only recognised in that first for loop.

Thanks

6 Answers 6

0

variable a scope is only withing the for loop.

If you want to access a in the while loop, you need to assign it to a global variable outside of both the loops.

Assuming you want to access each different values of a, then you need to store it in an array, which then doesnt really make any sense since you can just create an increasing order of ints.

But if that is what you want to do then here is what I would do, since piokuc already answered.

int [] global_a;
for (int a=0; a<definedgroups; a++)
{
  cout << "Enter Lower number for group " << a << ": "; 
  cin >> User_Groups [a] [0] ;
  gloabl_a[a] = a;
}

for (int i=0; i<global_a.size; i++){
   while(a > 0)
   {
   ...
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, scope of variable a is limited to the body of the for loop. If you want to reuse the variable / use it later, you can declare it before your for loop:

int a=0;
for (; a<definedgroups; a++)
{
  cout << "Enter Lower number for group " << a << ": "; 
  cin >> User_Groups [a] [0] ;
}
while(a > 0)
{
...
}

Comments

0

The a is declared in the scope of the first loop, so yes, it's only available there. To re-use it, you can declare it outside:

int a;
for (a=0; a<definedgroups; a++)
{
    cout << "Enter Lower number for group " << a << ": "; 
    cin >> User_Groups [a] [0] ;
}

Comments

0

the variable can only be used in nested loops.

if you want 2 differents loops using the same var, you should declare it before them both

Comments

0

The "a" is only in the scope of the first for loop.

To do what you want:

int a;
for(a = 0 ; ...)
{
  ...
}

while(a > 0) ...

Comments

0

When the for loop finishes, unless you broke out early, a == definedgroups is true.

So there is very little point in using 'the same variable'. You could create a new variable and start it off as being equal to definedgroups, and every compiler when told to optimize will use one variable for the other.

In fact, reusing the variable is bleeding needless state from one part of your code to another, and you should avoid it. Use a new variable, it won't cost you anything (unless you went off and took pointers to your loop index or somesuch)

It is quite possible that your actual problem is more complex than the toy one you demonstrated. You might want to post a self contained, simple, complete code example that demonstrates what you want to do.

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.