First of all, I'm sorry if there is something obvious that I'm missing, I only recently started to learn how to code.
I'm trying to create a die for a board game but the while
loop behaves weirdly and I couldn't figure out why. I would really appreciate it if someone could help me out.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int dice;
char input;
int game = 0;
srand(time(0));
while (game == 0) {
printf("\nRoll the dice? (Y/N)\n");
scanf("%c", &input);
if (input == 'Y') {
dice = (rand() % 6) + 1;
printf("\ndice: %d\n", dice);
}
}
return 0;
}
This is the output:
Roll the dice? (Y/N)
Y
dice: 2
Roll the dice? (Y/N)
Roll the dice? (Y/N)
Y
dice: 6
Roll the dice? (Y/N)
Roll the dice? (Y/N)