goto unsafe variable value

Asked

Viewed 35 times

-1

I’m making a simple code and I’m using the goto

#include <iostream>

using namespace std;

int exemplo(void);

int main()
{
    exemplo();
}

exemplo(){
comeco:
{
int numero;
numero = 0;
goto fim;
}
fim:
{
cout << numero;
}
}

but says that the numero was not declared in the brush and sometimes the code only assumes a random value for it, some solution? (I have several goto in that code)

  • But this code is to test the goto ? 'Cause I don’t see why I should goto here.

  • If the answer was useful to you, be sure to mark it, see: How and why to accept an answer?

1 answer

2


The scope in which you are declaring int numero;(comeco), is different from the scope in which it uses numero (fim).

So you will need to declare it in a scope above:

#include <iostream>

using namespace std;

int exemplo(void);

int main()
{
    exemplo();
}

int exemplo() {
    int numero;
    comeco:
    {
        numero = 0;
        goto fim;
    }
    fim:
    {
        cout << numero;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.