I made a mini dice game in C and for some reason i cant run my code. It's not showing me any errors when i compile. Help?
#include <stdio.h> //I/O
#include <stdlib.h> //rand() random number generator
#include <time.h> // time(NULL) seconds since 1 January 1970.
time for random generator
void delay() //dramatic pause
{
int ms = 1000;
clock_t time = clock();
while(clock() < time + ms);
}
int throwDice()
{
static int initialized = 0;
int number;
if(!initialized)
{
srand(time(NULL));
initialized=1;
}
number=rand()%6+1;
return number;
}
int main()
{
int humanWins=0, computerWins=0, humanToss, computerToss;
int numberOfTurns;
printf("Enter number of turns: ");
scanf("%d", numberOfTurns);
int turn;
for(turn=1; turn<=numberOfTurns; turn++)
{
char enter =0;
printf("\t\tTurn # %d\n", turn);
printf("\tPress enter to throw the dice.\n Rolling dice...");
printf("\tHUMAN\t||\tCOMPUTER");
do
{
//delay();
while(enter!= 'r'&&enter!='\n')
enter=getchar();
humanToss= throwDice();
computerToss= throwDice();
if(humanToss > computerToss)
{
humanWins++;
printf("\t %d \t||\t Human wins this round!",1,0);
}
else if(computerToss > humanToss)
{
computerWins++;
printf("\t %d \t||\t %d Computer wins this round!",0,1);
}
else if(humanToss == computerToss)
{
printf("\t\t\t ROUND WAS A TIE!!... Re-rolling dice...");
}
}while(humanToss==computerToss);
}
printf("The winner is..... ");
delay();
if(humanToss > computerToss)
printf("HUMAN!");
else
printf("COMPUTER! loser!");
return 0;
}
you didn't put ambracent symbol(&) while reading turn variable.you definitely put that.
Code:
#include <stdio.h> //I/O
#include <stdlib.h> //rand() random number generator
#include <time.h> // time(NULL) seconds since 1 January 1970.
time for random generator
void delay() //dramatic pause
{
int ms = 1000;
clock_t time = clock();
while(clock() < time + ms);
}
int throwDice()
{
static int initialized = 0;
int number;
if(!initialized)
{
srand(time(NULL));
initialized=1;
}
number=rand()%6+1;
return number;
}
int main()
{
int humanWins=0, computerWins=0, humanToss, computerToss;
int numberOfTurns;
printf("Enter number of turns: ");
scanf("%d",&numberOfTurns);
int turn;
for(turn=1; turn<=numberOfTurns; turn++)
{
char enter =0;
printf("\t\tTurn # %d\n", turn);
printf("\tPress enter to throw the dice.\n Rolling dice...");
printf("\tHUMAN\t||\tCOMPUTER");
do
{
//delay();
while(enter!= 'r'&&enter!='\n')
enter=getchar();
humanToss= throwDice();
computerToss= throwDice();
if(humanToss > computerToss)
{
humanWins++;
printf("\t %d \t||\t Human wins this round!",1,0);
}
else if(computerToss > humanToss)
{
computerWins++;
printf("\t %d \t||\t %d Computer wins this round!",0,1);
}
else if(humanToss == computerToss)
{
printf("\t\t\t ROUND WAS A TIE!!... Re-rolling dice...");
}
}while(humanToss==computerToss);
}
printf("The winner is..... ");
delay();
if(humanToss > computerToss)
printf("HUMAN!");
else
printf("COMPUTER! loser!");
return 0;
}