Challenge 2 Dice Roll Completed. Use 'srand' funct. for random generator

+1 Jeff T. · March 5, 2015
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

int main()

/*** Create a better non-repeating random number. This is MUCH better than the rand() function. Need to use the <time.h> for this function.
// It uses the computers clock to set the random number generator***/


int roll1 = 0, roll2 = 0, roll3 = 0, roll4 = 0, roll5 = 0, roll6 = 0;
char ans;
char again;

roll1 = (rand() % 6) + 1; //Create our rolls
roll2 = (rand() % 6) + 1;
roll3 = (rand() % 6) + 1;
roll4 = (rand() % 6) + 1;
roll5 = (rand() % 6) + 1;
roll6 = (rand() % 6) + 1;

int total = roll1 + roll2 + roll3; // Store roll totals
int total2 = roll4 + roll5 + roll6;

/*** This next line was used for debugging. Shows what the two rolls are to be able to input the correct or incorrect user input and test the output. Un-comment to use***/

//printf("Total = %d & Total2 = %d\n", total, total2);

printf("Roll 1 = %i \t", roll1); // Show first 3 rolls & total
printf("Roll 2 = %i \t", roll2);
printf("Roll 3 = %i \t\t", roll3);
printf("The total of the 3 rolls is %d\n\n", total);

printf("Do you think the next 3 rolls will be equal to, higher, or lower than the last 3 (Enter E, H or L)? "); // Ask user for input
scanf(" %c", &ans);

printf("\nRoll 4 = %i \t", roll4); // Show next 3 rolls & total
printf("Roll 5 = %i \t", roll5);
printf("Roll 6 = %i \t\t", roll6);
printf("The total for the next 3 rolls is %d\n\n", total2);

if(islower(ans)) ans = toupper(ans); // Convert user input to upper case letter if they entered it in lower case. Makes the next statements easier to evaluate and type.

// The following if/else if/else statements don't use {brackets}, they are not needed if only one line of code is used after the 'if' statement.

if ((ans == 'H' && total2 > total)|| (ans == 'L' && total2 < total)|| (ans == 'E' && total2 == total)) printf("You are CORRECT!!! Are you psychic or psycho?!? Nice Guess!!!\n\n");

else if ((ans == 'H' && total2 < total) || (ans == 'L' && total2 > total) || (ans == 'E' && total2 != total)) printf("WRONG!!! You are not psychic so you must be psycho!!! You Lost........\n\n");
else printf("Please enter an answer that makes sense next time... You are disqualified....Good Bye!\n\n");

printf("Would you like to play again (Y or N)? ");
scanf("\n %c", &again);
if(islower(again)) again = toupper(again);

if(again == 'Y') main();
else printf("Thanks for playing. Good Bye\n\n");

return 0;

Post a Reply


Oldest  Newest  Rating
0 Jeff T. · March 7, 2015
   Let me rephrase my last response, while I missed the newline character, I know it IS doing something when the code runs, and although it does not appear as a warning or error when the code compiles it's still read.When I re-ran the code with/ without the newline, and with/ without the space, I had no compile warnings/ errors or any problems with the program running. I ran each scenario about 10 times. As far as describing what is happening in all the examples, I believe when scanf reads in, it will read the newline as it's input and won't accept another keyboard input until the newline is cleared which the blank space does, but to respond correctly & intelligently, probably not at this point, it's beyond my knowledge of 'C', that's why I'm taking a C for Beginners course :) , ask me again in 6 months.
   I use a laptop with Windows 7 Home Premium.
0 Jeff T. · March 6, 2015
 It does nothing, just an over site from stuff which was deleted, since it did/ does nothing to fault or affect the program while it's running, or give any compile errors or warnings,  I didn't catch it while debugging. You did, nice catch. Thanks.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator