dimanche 29 janvier 2017

C random number producing garbage value

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

    srand(time(0)); 

    int random = 0, guess, guesses = 1;
    random = rand() % 100 + 1;
    printf("The number has been generated. Input 0 to quit.\n");
    do {
        scanf("%d", &guess); //takes user input for guess
        if (guess > random && guess != 0)
            printf("Lower!\n");
        else
        if (guess < random && guess != 0)
            printf("Higher!\n");
        else
        if (guess == random) {
            printf("Bingo!");
            return 0;
        } else
        if (guess == 0) {
            printf("Thanks for playing, the number was %d\n", &random);
            return 0;
        }
        guesses++;
    } while (guesses != 6); //maximum of 5 guesses

    printf("Thanks for playing, the number was %d\n", &random);

    return 0;
}

Whenever I display the random variable when it has already generated a number a garbage value will be outputted. But the code snippet works of checking the comparison between the guess and the random number, I just can't seem to output the random number correctly.




Aucun commentaire:

Enregistrer un commentaire