How to get a program running in C without looping and compiling again?

Asked

Viewed 137 times

1

# include <stdio.h>
int main ()
{ 
int Senha, Tentativa;
int Contador_De_Erros = 1;
printf("Vez do jogador 1 jogar\n\nLembrando que a senha tem que ser um numero entre 0 e 100\n\n");
printf("Qual sera a senha ? ");
scanf("%d",&Senha);
while (Senha < 0 || Senha > 100)
{
    printf("\nValor invalido, tente uma senha entre 0 e 100\n");
    printf("\nVez do jogador 1 jogar\n\nLembrando que a senha tem que ser um numero entre 0 e 100\n");
    printf("\nQual sera a senha ? ");
    scanf("%d",&Senha);
}
printf("\nVez do jogador 2 jogar\n\nQual a sua tentativa de advinhar o numero ? ");
scanf("%d",&Tentativa);
while (Tentativa < 0 || Tentativa > 100 )
{
    printf("\nValor invalido, tente uma tentativa entre 0 e 100\n");
    printf("\nVez do jogador 2 jogar\n\nQual a sua tentativa de advinhar o numero ? ");
    scanf("%d",&Tentativa);
} 
if (Tentativa == Senha - 1 || Tentativa == Senha + 1)
{
    printf("\nTa quente\n");
}
if (Tentativa < Senha && Tentativa != Senha - 1)
{
    printf("\nSua tentativa %d e menor que a senha, digite novamente a senha\n",Tentativa);
}
else if (Tentativa > Senha && Tentativa != Senha + 1)
{
    printf("\nSua tentativa %d e maior que a senha, digite novamente a senha\n",Tentativa);
}

while (Contador_De_Erros < 5 && Tentativa != Senha)
{   


    printf("\nVez do jogador 2 jogar\n\nQual a sua tentativa de advinhar o numero ? ");
    scanf("%d",&Tentativa);
    while (Tentativa < 0 || Tentativa > 100 )
    {
    printf("\nValor invalido, tente uma tentativa entre 0 e 100\n");
    printf("\nVez do jogador 2 jogar\n\nQual a sua tentativa de advinhar o numero ? ");
    scanf("%d",&Tentativa);
    }
    if (Tentativa == Senha - 1 || Tentativa == Senha + 1)
    {
        printf("\nTa quente\n");
    }
    if (Tentativa < Senha && Tentativa != Senha - 1)
    {
        printf("\nSua tentativa %d e menor que a senha, digite novamente a senha\n",Tentativa);
    }
    else if (Tentativa > Senha && Tentativa != Senha + 1)
    {
        printf("\nSua tentativa %d e maior que a senha, digite novamente a senha\n",Tentativa);
    }
    Contador_De_Erros += 1;
}
if (Tentativa == Senha)
{ 
    printf("\nVoce acertou ! o numero %d era a senha escolhida pelo jogador 1",Senha);
}
if (Tentativa != Senha)
{
    printf("\nVoce nao acertou a senha que era %d", Senha);
}

return 0;
}
int Volta_Tudo()
{
int Jogar_Novamente;
printf("Voce quer jogar novamente ?\n1 para SIM\n2 para Nao ");
scanf("%d",&Jogar_Novamente);
if (Jogar_Novamente == 1)
{
    goto main; 
}
return main;
}

I want to run that code again after its completion, there is some way to do it without loop and without compiling it again? This command goto would be a good method? If yes, how to treat this error when calling in it the function main? If not, what’s the best way to do it?

1 answer

0


Compiling has nothing to do with it.

In fact that goto is very wrong and does not compile (I hope not Compile on some crazy compiler, if compile I would not use it anymore), it does not make any sense. But it matters little because this function is even being called so in this specific code it doesn’t even have function to do it.

The goto should be avoided whenever possible and in this case it is possible. It may not even look great, but this is so bad that almost anything is better than this.

The whole code has some problems and escapes the pattern as people usually do. It may work, but that’s not how you program, be careful not to learn wrong. I think until the problem is poorly defined.

There are some possible solutions and the easiest and obvious in the way you started writing is to put all the code inside a loop, and you know how to do that because you’ve done it a few times. It is very simple, when you need something to be repeated it must be inside a loop, this mechanism serves for this.

If you do this then just somehow control the output of the loop. This loop can even be a while (1) which is an infinite loop, there inside it somewhere, in general at the end, you ask if you want to continue and depending on the answer will have a if to break the bond (break) and then finally go to the end of the execution. that last part you already did, but I would not do in another function.

It might even do it in another role, but it makes little sense there because several things you should do in another function in your code are not, and it would also be interesting to do the simple first. When master the repetition and control the output within the loop can sophisticate and abstract it to a function. It is not so difficult, but the if will have to be in the loop (unless direct control on while, but I don’t like it, in general it has to increase variable scope for no important gain, and it usually generates some confusions).

What you can’t do is call the function main() again as some do. In exercise it will work, but as soon as one starts making more complex codes it will trigger a stack overflow at some point.

I gave an answer that shows how.

Browser other questions tagged

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