1
0

manydice.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* manydice.c -- multiple dice rolls */
  2. /* compile with diceroll.c */
  3. #include <stdio.h>
  4. #include <stdlib.h> /* for library srand() */
  5. #include <time.h> /* for time() */
  6. #include "diceroll.h" /* for roll_n_dice() */
  7. /* and for roll_count */
  8. int main(void)
  9. {
  10. int dice,roll;
  11. int sides;
  12. int status;
  13. srand((unsigned int) time(0)); /* randomize seed */
  14. printf("Enter the number of sides per die, 0 to stop.\n");
  15. while (scanf("%d", &sides) == 1 && sides > 0 )
  16. {
  17. printf("How many dice?\n");
  18. if ((status =scanf("%d", &dice)) != 1)
  19. {
  20. if (status == EOF)
  21. break; /* exit loop */
  22. else
  23. {
  24. printf("You should have entered an integer.");
  25. printf(" Let's begin again.\n");
  26. while (getchar() != '\n')
  27. continue; /* dispose of bad input */
  28. printf("How many sides? Enter 0 to stop.\n");
  29. continue; /* new loop cycle */
  30. }
  31. }
  32. roll = roll_n_dice(dice, sides);
  33. printf("You have rolled a %d using %d %d-sided dice.\n",
  34. roll, dice, sides);
  35. printf("How many sides? Enter 0 to stop.\n");
  36. }
  37. printf("The rollem() function was called %d times.\n",
  38. roll_count); /* use extern variable */
  39. printf("GOOD FORTUNE TO YOU!\n");
  40. return 0;
  41. }