J
Jenny
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll(void);
enum status {CONTINUE, WIN, LOST};
int main()
{
int sum, mypoint;
enum status result;
sum = roll();
if( sum == 7|| sum == 11)
result = WIN;
else if( sum == 2 ||sum == 3||sum == 12)
result = LOST;
else
result = CONTINUE;
mypoint = sum;
while(result == CONTINUE){
sum = roll();
if(sum == mypoint)
result = WIN;
else if(sum == 7)
result = LOST;
else
result = CONTINUE;
}
if( result == WIN)
printf("The player win\n", result);
if( result == LOST)
printf("The player lost\n", result);
return 0;
}
int roll(void)
{
int die1, die2;
srand(time(NULL));
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
printf("Player rolled %d + %d = %d\n", die1, die2, die1 + die2);
return die1 + die2;
}
Good afternoon, everyone. I'm Kate.I am curious about where to place
the sentence:
srand ( time ( NULL ) );
In this program, if I place it in function ------- int roll
( void ) ,I always get a wrong result, such as:
4 + 1 = 5; 4 + 5 = 9;
4 + 1 = 5; 4 + 5 = 9
The player win. The player
win.
Press any key to continue. Press any key to
continue.
The player can win every time.
But if I place the sentence in function ------- int main (), the
program executes successfully.
I don't quite understand .Can you help me ?
#include <stdlib.h>
#include <time.h>
int roll(void);
enum status {CONTINUE, WIN, LOST};
int main()
{
int sum, mypoint;
enum status result;
sum = roll();
if( sum == 7|| sum == 11)
result = WIN;
else if( sum == 2 ||sum == 3||sum == 12)
result = LOST;
else
result = CONTINUE;
mypoint = sum;
while(result == CONTINUE){
sum = roll();
if(sum == mypoint)
result = WIN;
else if(sum == 7)
result = LOST;
else
result = CONTINUE;
}
if( result == WIN)
printf("The player win\n", result);
if( result == LOST)
printf("The player lost\n", result);
return 0;
}
int roll(void)
{
int die1, die2;
srand(time(NULL));
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
printf("Player rolled %d + %d = %d\n", die1, die2, die1 + die2);
return die1 + die2;
}
Good afternoon, everyone. I'm Kate.I am curious about where to place
the sentence:
srand ( time ( NULL ) );
In this program, if I place it in function ------- int roll
( void ) ,I always get a wrong result, such as:
4 + 1 = 5; 4 + 5 = 9;
4 + 1 = 5; 4 + 5 = 9
The player win. The player
win.
Press any key to continue. Press any key to
continue.
The player can win every time.
But if I place the sentence in function ------- int main (), the
program executes successfully.
I don't quite understand .Can you help me ?