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

 Jeff T. · March 5, 2015 #include #include #include #include int main(){ /*** Create a better non-repeating random number. This is MUCH better than the rand() function. Need to use the for this function. // It uses the computers clock to set the random number generator***/ srand(time(NULL)); 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); getchar(); 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); getchar(); if(islower(again)) again = toupper(again); if(again == 'Y') main(); else printf("Thanks for playing. Good Bye\n\n"); return 0;}

Replies

 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. 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.
• 1

C

130,987 followers