fork download
  1. // Attached: HW_2a
  2. // ===========================================================
  3. // File: HW_2a
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <limits>
  12. using namespace std;
  13.  
  14. // Function prototypes
  15. void getScores(double scores[], int size);
  16. void showMenu();
  17. char getChoice();
  18. void displayResult(char choice, double scores[], int size);
  19. void clearScreen();
  20.  
  21. int main()
  22. {
  23. const int SIZE = 5;
  24. double testScores[SIZE];
  25. char choice;
  26.  
  27. getScores(testScores, SIZE);
  28.  
  29. clearScreen();
  30. showMenu();
  31.  
  32. choice = getChoice();
  33.  
  34. displayResult(choice, testScores, SIZE);
  35.  
  36. return 0;
  37. }
  38.  
  39. // ===========================================================
  40.  
  41. void getScores(double scores[], int size)
  42. {
  43. cout << "Enter 5 test scores:\n";
  44.  
  45. for (int i = 0; i < size; i++)
  46. {
  47. cin >> scores[i];
  48. }
  49. }
  50.  
  51. // ===========================================================
  52.  
  53. void showMenu()
  54. {
  55. cout << "A.) Calculate the average of the test scores.\n";
  56. cout << "B.) Display all test scores\n";
  57. }
  58.  
  59. // ===========================================================
  60.  
  61. char getChoice()
  62. {
  63. char choice;
  64. cout << "Enter your choice: ";
  65. cin >> choice;
  66. return choice;
  67. }
  68.  
  69. // ===========================================================
  70.  
  71. void displayResult(char choice, double scores[], int size)
  72. {
  73. clearScreen();
  74.  
  75. switch (choice)
  76. {
  77. case 'A':
  78. case 'a':
  79. {
  80. double sum = 0;
  81.  
  82. for (int i = 0; i < size; i++)
  83. sum += scores[i];
  84.  
  85. double average = sum / size;
  86.  
  87. cout << fixed << setprecision(2);
  88. cout << "The average is " << average << endl;
  89. break;
  90. }
  91.  
  92. case 'B':
  93. case 'b':
  94. {
  95. cout << "Test scores:\n";
  96. cout << fixed << setprecision(2);
  97.  
  98. for (int i = 0; i < size; i++)
  99. cout << scores[i] << endl;
  100. break;
  101. }
  102.  
  103. default:
  104. cout << "Invalid entry!" << endl;
  105. }
  106.  
  107. cout << "Press any key to continue . . .";
  108. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  109. cin.get();
  110. }
  111.  
  112. // ===========================================================
  113.  
  114. void clearScreen()
  115. {
  116. system("cls"); // Windows
  117. // system("clear"); // Mac/Linux
  118. }
  119.  
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
99
77
86
59
92
A
stdout
Enter 5 test scores:
A.) Calculate the average of the test scores.
B.) Display all test scores
Enter your choice: The average is 82.60
Press any key to continue . . .
stderr
sh: 1: cls: not found
sh: 1: cls: not found