Challenge 2 - dice game

+1 Abhinav Mahajan · August 16, 2014
none if the 'if' and 'else if' statements are getting executed :(

//Challenge 2: Make a dice game, where user rolls 3 dice, sum calculated, and after that guess whether next roll will be higher, lower or same

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
int dice1;
int dice2;
int dice3;
int sum1;
char input;
int secondRoll1;
int secondRoll2;
int secondRoll3;
int sum2;

printf("Dice are rolling...\n");
printf("Rolled numbers are: %d, %d, %d\n", dice1 = (rand()%6) + 1, dice2 = (rand()%6) + 1, dice3 = (rand()%6) + 1);
printf("Your total is: %d\n\n", sum1 = dice1+dice2+dice3);

printf("Enter whether next roll will be high, low or same: ");
scanf(" %s", &input);

printf("\n\nDice are rolling...\n");
printf("Rolled numbers are: %d, %d, %d\n", secondRoll1 = (rand()%6) + 1, secondRoll2 = (rand()%6) + 1, secondRoll3 = (rand()%6) + 1);
printf("Your total is: %d\n\n", sum2 = secondRoll1+secondRoll2+secondRoll3);

if(sum1 == sum2 && input == "same") 
{
printf("You win!");
}
else if(sum1 < sum2 && input == "high")
{
printf("You win!");
}
else if(sum1 > sum2 && input == "low")
{
printf("You win!");
}

else if(sum1 == sum2 && input == "high") 
{
printf("You lose!");
}
else if(sum1 == sum2 && input == "low")
{
printf("You lose!");
}

else if(sum1 < sum2 && input == "same") 
{
printf("You lose!");
}
else if(sum1 < sum2 && input ==  "low")
{
printf("You lose!");
}

else if(sum1 > sum2 && input == "same") 
{
printf("You lose!");
}
else if(sum1 > sum2 && input == "high")
{
printf("You lose!");
}
else
{
printf("Try again!");
}

return 0;

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Brandon Elliott · August 16, 2014
probably because you are trying to set the values of the dice from within the printf line?

printf is for outputting text. you shouldn't be setting variables in it

also your if else statements could be nested better

also you should initialize your int variables to 0.

oh and your input should be a string, not a char.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
int dice1 = 0, dice2 = 0, dice3 = 0;
int sum1 = 0, sum2 = 0;
string input;
int secondRoll1 = 0, secondRoll2 = 0, secondRoll3 = 0;

printf("Dice are rolling...\n");
dice1 = (rand()%6) + 1;
dice2 = (rand()%6) + 1;
dice3 = (rand()%6) + 1;
printf("Rolled numbers are: %d, %d, %d\n", dice1, dice2, dice3);
sum1 = dice1 + dice2 + dice3;
printf("Your total is: %d\n\n", sum1);

printf("Enter whether next roll will be high, low or same: ");
scanf("%s", input);

printf("\n\nDice are rolling...\n");
secondRoll1 = (rand()%6) + 1;
secondRoll2 = (rand()%6) + 1;
secondRoll3 = (rand()%6) + 1;
printf("Rolled numbers are: %d, %d, %d\n", secondRoll1, secondRoll2, secondRoll3);
sum2 = secondRoll1 + secondRoll2 + secondRoll3;
printf("Your total is: %d\n\n", sum2);

if(sum1 == sum2 && input == "same")
{
printf("You win!");
}
else if(sum1 < sum2 && input == "high")
{
printf("You win!");
}
else if(sum1 > sum2 && input == "low")
{
printf("You win!");
}

else if(sum1 == sum2 && input == "high")
{
printf("You lose!");
}
else if(sum1 == sum2 && input == "low")
{
printf("You lose!");
}

else if(sum1 < sum2 && input == "same")
{
printf("You lose!");
}
else if(sum1 < sum2 && input == "low")
{
printf("You lose!");
}

else if(sum1 > sum2 && input == "same")
{
printf("You lose!");
}
else if(sum1 > sum2 && input == "high")
{
printf("You lose!");
}
else
{
printf("Try again!");
}

return 0;

}

there are still some compiling errors but i'll let you figure those out
  • 1

C

107,009 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator