0

A normal six-sided dice is thrown six times; the user must guess a number each time the dice is thrown. If the number matches the guess, the user wins a point. Score 4 points to win. I'm working on a project to make a dice throwing game. The goal is to guess the number that the dice is going to land on, and to repeat this loop until the user chooses to exit the program. Currently I'm just working on getting the inital stage to work, but for some reason my dice is coming up with an extremely large number and due to my limited understanding of srand I don't know how to fix it, so any help would be greatly appreciated

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

int main()
{
    int runs, correct, correct_guesses, guess;

    srand(time(NULL));
    correct_guesses = 0;

    int roll = ((rand() % 6) + 1);
    printf("Please guess the number the dice will land on\n");
    printf("%d", &roll);
    scanf("%d", &guess);
    {
        switch (guess)
        {
        case '1': correct = 1 == roll; break;
        case '2': correct = 2 == roll; break;
        case '3': correct = 3 == roll; break;
        case '4': correct = 4 == roll; break;
        case '5': correct = 5 == roll; break;
        case '6': correct = 6 == roll; break;
        default: correct = 0; printf("Not a possible outcome\n");
        }
    if (correct)
    {
        printf("You guessed correctly!\n");
    }
    else
        printf("You guessed incorrectly\n");
    }
}
  • 10
    `printf("%d", &roll);` should be `printf("%d", roll);` - turn on compiler warnings and treat them as errors – UnholySheep Dec 27 '19 at 15:36
  • 2
    If your understanding of some C functions are limited, I would recomand you to use the man command in one of your terminal: man srand web link to this man : https://linux.die.net/man/3/srand Looking at your code the issue is not directly linked to the srand usage. You are trying to display the adresse of your rand and guess int variable. I would also recommand you to have a deep look to this question : https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in – pix Dec 27 '19 at 15:38
  • 1
    Why use a switch? Just write `correct = guess == roll`, or just `if( guess == roll) correct = 1`. There's no virtue in excessive brevity. – William Pursell Dec 27 '19 at 16:38

0 Answers0