C program-dice rolling program

2011-05-05 6:31 am
#include <stdlib.h>
#include <stdio.h>
#include <time.h>main()
{
int dice,ran_dice,point=100;
int choice='Y';
printf("You got %d point.\n", point); // output point
while(choice=='Y'&& point!=0) {
printf("Please guess the dice number:"); // prompt user to input their guess
scanf("%d",&dice);
srand(time(NULL));
ran_dice = rand()%6+1; // generate random dice number
if (dice<1 || dice>6){
printf("Wrong input ! Please enter digit 1 to 6\n");
printf("Please guess the dice number:"); // prompt user to input their guess
scanf("%d",&dice);
srand(time(NULL));
ran_dice = rand()%6+1;}
printf("The dice is %d\n",ran_dice); // output result
if (dice==ran_dice) // compare the user's guess and the result
{printf("You win!\n"); // winning output
point=point+20;
printf("You have %d points.\n",point);}
else
{printf("Wrong guess!\n"); //lossing output
point=point-20;
printf("You have %d points.\n",point);}
printf("Do you want to continue(Y or N)?");
scanf("%c\n",&choice);
}
if (point==0)
printf("Sorry you have lost all your point, the game is over.\n");
system("PAUSE");
}

Above is a program I wrote but it is unable to loop. Can anyone pls solve the problem?

回答 (2)

2011-05-05 8:40 am
✔ 最佳答案
...
scanf("\n%c",&choice); //Originally, you read the newline
}
if (point==0)
printf("Sorry you have lost all your point, the game is over.\n");
system("PAUSE");
}
2011-05-05 10:30 am
Some unrelated comments...
1. srand() only needs to be called once, it's not really useful to call it every time before rand().
2. Using gets() instead of scanf() can reduces a number of headaches. You have to parse the string yourself afterwards though.

2011-05-05 02:30:42 補充:
3. Shouldn't choice be a char instead of an int?


收錄日期: 2021-04-13 18:11:55
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20110504000051KK01219

檢視 Wayback Machine 備份