jeudi 5 juillet 2018

Why is my rand() always kicking out the same number? [duplicate]

This question already has an answer here:

When I say the same number, I mean it is always 1 + the minumum number. I am trying to teach myself c++, so don't go to hard on me :).

#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int complete = 0;

int game() {
    cout << "Ok then. So I have picked a number between 0 and 20. You have 5 guesses." << endl;

    int randInt = rand() % 20 + 1;
    cout << randInt << endl;
    int guess;
    cin >> guess;
    for (int attempt = 4; attempt > 1; attempt--) {
        if (guess == randInt) {
            cout << "You guessed correctly, " << randInt << " was the answer!" << endl;
            complete = 1;
            return complete;
        }
        else if (guess > randInt) {
            cout << "The number is less than " << guess << ". You have " << attempt << " attempts left." << endl;
            cin >> guess;
        }
        else if (guess < randInt) {
            cout << "The number is greater than " << guess << ". You have " << attempt << " attempts left." << endl;
            cin >> guess;
        }
    }
    cout << "Sorry, but the answer was " << randInt << endl;
    complete = 1;
    return complete;
}

Any help would be appreciated. Thanks




Aucun commentaire:

Enregistrer un commentaire