Home > Enterprise >  Making a simple guessing game between 0 and 100, but game ends after 2 guesses
Making a simple guessing game between 0 and 100, but game ends after 2 guesses

Time:11-13

the code is in swedish btw.

int nyttal(int n){
    int nyttal = rand() % 100   1;
    return rand() % nyttal;
}

    //Lak Lägg

void utforEnSpelomgang(){
    const int n =100;
    const int datornstal = nyttal(n);
    int antalUtfardaGissningar = 0;  //number of guesses made by user

    cout << "Datorn tänker på ett tal mellan noll och " << n << ". Gissa vilket!" << endl;
    int g;
    cin >> g;


    if(g < datornstal){             //if g is smaller than the programs number it will tell you that your guess is smaller
        cout << "Mindre" << endl;
          antalUtfardaGissningar;
        cout << "Antal utfärde gissningar: " << antalUtfardaGissningar << endl;
        cin >> g;

    }
    if(g > datornstal){     //user guessed a bigger number
        cout << "Större" << endl; 
          antalUtfardaGissningar;
        cout << "Antal utfärda gissningar: " << antalUtfardaGissningar << endl;
        cin >> g;


    }
    if(g == datornstal){
        cout << "Du gissade rätt" << endl;
          antalUtfardaGissningar;
        cout << "Antal utfärda gissningar: " << antalUtfardaGissningar << endl;
    }
    //Lägg
}

Everytime i run the code the game ends after 2 guesses and on the second one doesnt even output the cout line ive written. How do i get the code to keep running until the user has guessed right?

CodePudding user response:

You can use/add a while loop as shown

void utforEnSpelomgang(){
    const int n =100;
    const int datornstal = nyttal(n);
    int antalUtfardaGissningar = 0;

    cout << "Datorn tänker på ett tal mellan noll och " << n << ". Gissa vilket!" << endl;
    int g;
    cin >> g;

    while(g!= datornstal)//added this
        {
        if(g < datornstal){             //if g is smaller than the programs number it will tell you that your guess is smaller
            cout << "Mindre" << endl;
              antalUtfardaGissningar;
            cout << "Antal utfärde gissningar: " << antalUtfardaGissningar << endl;
            cin >> g;
    
        }
        if(g > datornstal){     //user guessed a bigger number
            cout << "Större" << endl; 
              antalUtfardaGissningar;
            cout << "Antal utfärda gissningar: " << antalUtfardaGissningar << endl;
            cin >> g;
    
    
        }
        if(g == datornstal){
            cout << "Du gissade rätt" << endl;
              antalUtfardaGissningar;
            cout << "Antal utfärda gissningar: " << antalUtfardaGissningar << endl;
            break;//added this
        }
    }
    //Lägg
}

In the above program i have added 2 things. First is a while and second is the break statement for breaking out of the while loop in case the guess is correct.

Also if you're wondering why always the same number is generated take a look at How to generate a random number in C ?

Note that the last if is redundant and so you can remove it and put the code inside the last if outside the while loop as shown below:

 while(g!= datornstal)//added this
        {
        if(g < datornstal){             //if g is smaller than the programs number it will tell you that your guess is smaller
            cout << "Mindre" << endl;
              antalUtfardaGissningar;
            cout << "Antal utfärde gissningar: " << antalUtfardaGissningar << endl;
            cin >> g;
    
        }
        else if(g > datornstal){     //user guessed a bigger number
            cout << "Större" << endl; 
              antalUtfardaGissningar;
            cout << "Antal utfärda gissningar: " << antalUtfardaGissningar << endl;
            cin >> g;
    
    
        }
       
    }
     cout << "Du gissade rätt" << endl;
       antalUtfardaGissningar;
    cout << "Antal utfärda gissningar: " << antalUtfardaGissningar << endl;
  •  Tags:  
  • c
  • Related