cingolf.cpp 831 B

1234567891011121314151617181920212223242526272829303132
  1. // cingolf.cpp -- non-numeric input skipped
  2. #include <iostream>
  3. const int Max = 5;
  4. int main()
  5. {
  6. using namespace std;
  7. // get data
  8. int golf[Max];
  9. cout << "Please enter your golf scores.\n";
  10. cout << "You must enter " << Max << " rounds.\n";
  11. int i;
  12. for (i = 0; i < Max; i++)
  13. {
  14. cout << "round #" << i+1 << ": ";
  15. while (!(cin >> golf[i])) {
  16. cin.clear(); // reset input
  17. while (cin.get() != '\n')
  18. continue; // get rid of bad input
  19. cout << "Please enter a number: ";
  20. }
  21. }
  22. // calculate average
  23. double total = 0.0;
  24. for (i = 0; i < Max; i++)
  25. total += golf[i];
  26. // report results
  27. cout << total / Max << " = average score "
  28. << Max << " rounds\n";
  29. // cin.get();
  30. // cin.get();
  31. return 0;
  32. }