fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <cstdlib>
  6. int rollDice();
  7. using namespace std;
  8. int total=0;
  9. int main()
  10. {
  11. int roll;
  12. srand(time(0));
  13. cout << setw(10) << "Roll" << setw(10) << "Total" << endl;
  14. cout << setw(10) << "----" << setw(10) << "-----" << endl;
  15. while (total<25)
  16. {
  17. roll = rollDice();
  18. cout << setw(10) << roll << setw(10) << total << endl;
  19. }
  20. return 0;
  21. }
  22.  
  23.  
  24. // =============== rollDice ===============
  25.  
  26. int rollDice()
  27. {
  28. int i;
  29. i=1+(rand()%12);// 2 dice with 6 sides (1-12)
  30. if(i%2==0){
  31. total= total+i; // if even, add to total
  32. }
  33. else{
  34. total= total-i; // if odd, subtract from total
  35. }
  36. return i;
  37. }
  38.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
      Roll     Total
      ----     -----
         1        -1
        12        11
         3         8
        11        -3
        11       -14
        11       -25
         6       -19
         7       -26
         8       -18
         8       -10
        12         2
         5        -3
         7       -10
         4        -6
         9       -15
         1       -16
        12        -4
         5        -9
         2        -7
        10         3
         7        -4
         1        -5
         6         1
         5        -4
         5        -9
         8        -1
         5        -6
         5       -11
         2        -9
         5       -14
         2       -12
         2       -10
         9       -19
         4       -15
        12        -3
         7       -10
         7       -17
        10        -7
         5       -12
         2       -10
         9       -19
         4       -15
         7       -22
         4       -18
        11       -29
         7       -36
         8       -28
        10       -18
        11       -29
        10       -19
        12        -7
         5       -12
        10        -2
         5        -7
        10         3
         6         9
        12        21
         6        27