vendredi 28 août 2020

Getting same output everytime [closed]

This a simple number guessing game. If you guessed the number right, it outputs "You win!!!", but if the number of tries (numberofguesses) is exceeded, it should output "You lose", but it is showing "You win!!!" even though I checked the values of numberofguesses, secretnum and guess after the while loop. Answer in simple words, I'm a beginner.

#include <iostream>
using namespace std;

int main()
{
    int secretnum = 7;
    int guess = 0;
    int numberofguesses = 3;
    while (secretnum != guess && numberofguesses != 0) {
        cout << "enter your guess: ";
        cin >> guess;
        --numberofguesses;
    }
    if (secretnum = guess && numberofguesses != 0) {
        cout << "You win!!!";
    }
    else
    {
        cout << "You lose";
    }
}

Aucun commentaire:

Enregistrer un commentaire