0

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?

1 Answers1

0

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;
Jason
  • 36,170
  • 5
  • 26
  • 60
  • Your answer solved my original question but now for some reason everytime i run the code the right answer is always 29, why is that? – Ahmad Halabi Nov 12 '21 at 17:12
  • @AhmadHalabi Take a look at the link that i have given at the end of my answer for generating a random number in C++. Random number generation is a whole different topic/question. You can find the answer at that link. Also, if my answer solved your original question, can you mark this as correct. – Jason Nov 12 '21 at 17:15
  • You didn't set the seed on your random number generator so it picked a fixed seed that will be used every time you run your code. With the same seed you get the same random number sequence. Note that you want to seed your random number generator 1 time at the beginning of main. Don't make the mistake of seeding every time you want a random number. – drescherjm Nov 12 '21 at 17:16
  • You could actually remove the last if statement that checks if it is equal to the number because it would exit on the next iteration, and so is not necessary. What you could do is put the code in the last if statement outside of the code, and then remove the statement altogether as the loop will break on the next iteration. – 2pichar Nov 12 '21 at 17:29
  • i cant figure out what a seed is and where its suppose to go in my code – Ahmad Halabi Nov 12 '21 at 17:30
  • @AhmadHalabi You can ask about what is a seed and what it does in a separate/new question. There someone can explain it in detail. Feel free to ask a new question if you think that will help you. – Jason Nov 12 '21 at 17:40