Home > Net >  Problem with Goto statement in C Working the First Time but Not the Second with the Same Code
Problem with Goto statement in C Working the First Time but Not the Second with the Same Code

Time:11-20

So I coded a little guessing game, just to practice for loops , here is the code:

#include <stdio.h>
void main(){
    int rep=15 ;int essay=3 ;int  choix=0 ; char c2 ;
OUT:
    for (;essay>0;essay--)
    {
        printf("\nEntrer un numero entre 1 et 20:");
        scanf("%d" , &choix);
        if (choix == rep)
        {
            printf("VOUS AVEZ GAGNEZ!:)\n");
            printf("Voulez vous rejouer?O/N:");
            scanf(" %c" , &c2);
            if (c2 == 'O' || c2 == 'o')
            {
                goto OUT;
            }   //  break;
            else
                return;

        }

        if (choix<1 || choix>20)
        {
            printf("Entrer un numero entre 1 et 20 !\n");
            essay  = 1;
        }
        else
            printf("Mauvais choix! :(\n");
    }
    printf("Voulez vous rejouer?O/N:");
    scanf(" %c" , &c2);
    if (c2 == 'O' || c2 == 'o')
    {
        goto OUT;
    }   //  break;
    else
        return;
}

Work very good except for the last bit of code :

    printf("Voulez vous rejouer?O/N:");
    scanf(" %c" , &c2);
    if (c2 == 'O' || c2 == 'o')
    {
        goto OUT;
    }   //  break;
    else
        return;

At the end of the game when you are out of guess u can try Again if you want by typing 'O' or 'o' for yes and 'n' or 'N' to end the game same thing when you win . The problem come up at the end when you win you can restart the game or end it with no problem . But if you lose even if you type 'o' or 'O' the game wont restart (and it is the same code as in the if statement when you win). Here is the output, it is looking like that when you win and want to restart :

Entrer un numero entre 1 et 20:15
VOUS AVEZ GAGNEZ!:)
Voulez vous rejouer?O/N:O

Entrer un numero entre 1 et 20:

But when you lose it look like that(note when you lose if you press 'n' or 'N' the game stop) :

Entrer un numero entre 1 et 20:15
VOUS AVEZ GAGNEZ!:)
Voulez vous rejouer?O/N:O

Entrer un numero entre 1 et 20:10
Mauvais choix! :(

Entrer un numero entre 1 et 20:10
Mauvais choix! :(

Entrer un numero entre 1 et 20:10
Mauvais choix! :(
Voulez vous rejouer?O/N:o
Voulez vous rejouer?O/N:o
Voulez vous rejouer?O/N:o
Voulez vous rejouer?O/N:

So you can see this is very weird and i dont understand why as in the two if statement it's the same code !

CodePudding user response:

You initialize int essay=3; before the label OUT. When you goto OUT; essay is still 0. The easy fix is to move initialization into the loop:

    for(int essay = 3; essay > 0; essay--) {

This is how I refactored your program in case it helps:

  • check for scanf failure
  • use define for magic values
  • eliminated goto in favor of a double loop
  • use break and continue in inner loop, and only increment eassy when needed so there is no need to undo for "bad" choices.
  • count up instead of down (by convention).
  • tolower instead testing for case differences
  • reformatted prompts for readability
  • moved variable definitions close to use (easier to figure out out where used and minimizes scope)
  • eliminated duplicate retry code
#include <ctype.h>
#include <stdio.h>

#define CHECK(p, m) if(!(p)) { printf("%s:%d: %s\n", __FILE__, __LINE__, m); return 1; };
#define MAX_TRIES 3
#define REP 15

int main() {
    for(;;) {
        for(int essay = 0; essay < MAX_TRIES;) {
            printf("\nEntrer un numero entre 1 et 20: ");
            int choix;
            CHECK(scanf("%d" , &choix) != EOF, "scanf failed\n");
            if(choix == REP) {
                printf("VOUS AVEZ GAGNEZ!:)\n");
                break;
            }
            if(choix < 1 || choix > 20) {
                printf("Entrer un numero entre 1 et 20 !\n");
                continue;
            }
            printf("Mauvais choix! :(\n");
            essay  ;
        }
        printf("Voulez vous rejouer (O/N)? ");
        char c2;
        CHECK(scanf(" %c" , &c2) != EOF, "scanf failed\n");
        c2 = tolower(c2);
        if(c2 != 'o')
            break;
    }
}

  •  Tags:  
  • c
  • Related