C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

// Roll the dice a second time to get your second total


dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total2 = dice1 + dice2;


// Display the second total for the user


printf("\nThe second roll was %d and %d, ", dice1, dice2);
printf("for a total of %d.\n\n", total2);


// Now compare the two dice totals against the user's guess
// and tell them if they were right or not.


if (ans == 'L')
{
if (total2 < total1)
{
printf("Good job! You were right!\n");
printf("%d is lower than %d\n", total2, total1);
}
else
{
printf("Sorry! %d is not lower than %d\n\n", total2,
total1);
}
}
else if (ans == 'H')
{
if (total2 > total1)
{
printf("Good job! You were right!\n");
printf("%d is higher than %d\n", total2, total1);
}
else
{
printf("Sorry! %d is not higher than %d\n\n", total2,
total1);
}
}
else if (ans == 'S')
{
if (total2 == total1)
{
printf("Good job! You were right!\n");
printf("%d is the same as %d\n\n", total2, total1);
}
else
{
printf("Sorry! %d is not the same as %d\n\n",
total2, total1);
}
}


return(0);
}

Free download pdf