fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main() {
  9. int points = 0;
  10. int roundsPlayed = 0;
  11. const int MAX_ROUNDS = 10;
  12. const int WIN_POINTS = 500;
  13. const int LOSE_POINTS = 200;
  14.  
  15. srand(time(0));
  16.  
  17. cout << "starting points: " << points << endl;
  18.  
  19. for (int i = 1; i<= MAX_ROUNDS; i++) {
  20. int roundScore = rand() % 101;
  21. points += roundScore;
  22. roundsPlayed++;
  23.  
  24. cout << "round " << i << ": scored " << roundScore << " points, total = "
  25. << points << endl;
  26.  
  27. if (points >= WIN_POINTS) {
  28. cout << "you win! you reached " << points << " points." << endl;
  29. break;
  30. }
  31.  
  32. if (points < LOSE_POINTS && i == MAX_ROUNDS) {
  33. cout << "you lose! too few points." << endl;
  34. break;
  35. }
  36. }
  37.  
  38. double average = static_cast<double>(points) / roundsPlayed;
  39.  
  40. cout << "\ngame over." << endl;
  41. cout << "total points: " << points << endl;
  42. cout << "average per round: " << average << endl;
  43.  
  44. return 0;
  45.  
  46. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
starting points: 0
round 1: scored 35 points, total = 35
round 2: scored 90 points, total = 125
round 3: scored 84 points, total = 209
round 4: scored 27 points, total = 236
round 5: scored 14 points, total = 250
round 6: scored 55 points, total = 305
round 7: scored 91 points, total = 396
round 8: scored 87 points, total = 483
round 9: scored 98 points, total = 581
you win! you reached 581 points.

game over.
total points: 581
average per round: 64.5556