Chapter19ex2.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. main(void)
  7. {
  8. int dice1, dice2;
  9. int total1, total2;
  10. time_t t;
  11. char ans;
  12. /* this is needed to make sure each number is actually random */
  13. srand(time(&t));
  14. dice1 = (rand() % 5) + 1;
  15. dice2 = (rand() % 5) + 1;
  16. total1 = dice1 + dice2;
  17. printf("First roll of the dice was %d and %d, ", dice1, dice2);
  18. printf("for a total of %d.\n\n\n", total1);
  19. do {
  20. puts("Do you think the next roll will be ");
  21. puts("(H)igher, (L)ower, or (S)ame?\n");
  22. puts("Enter H, L, or S to reflect your guess.");
  23. scanf(" %c", &ans);
  24. ans = toupper(ans);
  25. } while ((ans != 'H') && (ans != 'L') && (ans != 'S'));
  26. dice1 = (rand() % 5) + 1;
  27. dice2 = (rand() % 5) + 1;
  28. total2 = dice1 + dice2;
  29. printf("\nThe second roll was %d and %d, ", dice1, dice2);
  30. printf("for a total of %d.\n\n", total2);
  31. if (ans == 'L')
  32. {
  33. if (total2 < total1)
  34. {
  35. printf("Good job! You were right!\n");
  36. printf("%d is lower than %d\n", total2, total1);
  37. }
  38. else
  39. {
  40. printf("Sorry! %d is not lower than %d\n\n", total2,
  41. total1);
  42. }
  43. }
  44. else if (ans == 'H')
  45. {
  46. if (total2 > total1)
  47. {
  48. printf("Good job! You were right!\n");
  49. printf("%d is higher than %d\n", total2, total1);
  50. }
  51. }
  52. else if (ans == 'S')
  53. {
  54. if (total2 == total1)
  55. {
  56. printf("Good job! You were right!\n");
  57. printf("%d is the same as %d\n\n", total2, total1);
  58. }
  59. else
  60. {
  61. printf("Sorry! %d is not the same as %d\n\n", total2,
  62. total1);
  63. }
  64. }
  65. return(0);
  66. }