Here is my code, im trying to call the variable defined inside the main(), but outside the current scope:
#include<iostream>
int main()
{
int asd = 10; //***
while (True)
{
int asd = 100;
asd -= 1; //***is it possible to use the 'asd' defined before while loop
if (asd ==0) break;
}
}
best regards Eason
No. The int asd = 100;
is masking the old asd
variable.
What you want (I suppose) is to just assign the value 100 to asd
, which (and I'm sure you know this) you could simply do by writing asd = 100;
.
There is, of course, one more issue: you would want to do that before the while
loop - otherwise, you're going to have an infinite loop because at the beginning of every iteration the value of asd
will be 100.
You're missing a ;
after asd = 100
, by the way.