fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. void print_menu();
  8. int get_selection();
  9. std::string get_Name();
  10. float get_GPA();
  11. int get_Year();
  12. void input_new_student(std::string student_names[], float student_GPA[], int student_start_year[], int index, int ramapo_id[]);
  13. void print_all(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, int ramapo_id[]);
  14. void print_by_year(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size);
  15. void print_statistics(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, float sum);
  16.  
  17.  
  18. using namespace std;
  19.  
  20. int main()
  21. {
  22. std::string student_names[100];
  23. float student_GPA[100];
  24. int student_start_year[100];
  25. int ramapo_id[100];
  26. int userChoice;
  27. int index = 0;
  28. int size = 0;
  29. float sum = 0.0;
  30.  
  31. do
  32. {
  33. print_menu();
  34. userChoice = get_selection();
  35. if (userChoice == 1)
  36. {
  37. input_new_student(student_names, student_GPA, student_start_year, index, ramapo_id);
  38. index++;
  39. }
  40. if (userChoice == 2)
  41. {
  42. print_all(student_names, student_GPA, student_start_year, index, size, ramapo_id);
  43. }
  44. if (userChoice == 3)
  45. {
  46. print_by_year(student_names, student_GPA, student_start_year, index, size);
  47. }
  48. if (userChoice == 4)
  49. {
  50. print_statistics(student_names, student_GPA, student_start_year, index, size, sum);
  51. }
  52. if (userChoice == 5)
  53. {
  54. return 0;
  55. }
  56. } while (false);
  57. return 0;
  58. }
  59. void print_menu()
  60. {
  61. cout << "Please pick from the following menu " << endl;
  62. cout << "1. Add a new student " << endl;
  63. cout << "2. Print all students " << endl;
  64. cout << "3. Print students by year " << endl;
  65. cout << "4. Print student statistics " << endl;
  66. cout << "5. Quit" << endl;
  67. }
  68. int get_selection()
  69. {
  70. int userChoice;
  71. cin >> userChoice;
  72.  
  73. if (userChoice > 4 || userChoice < 1)
  74. {
  75. cout << "Error: Invalid input " << userChoice << ", please try again: ";
  76. }
  77. return userChoice;
  78. }
  79. string get_Name()
  80. {
  81. std::string student_name;
  82.  
  83. cout << "Please enter the student's name: ";
  84. cin >> student_name;
  85.  
  86. return student_name;
  87. }
  88. float get_GPA()
  89. {
  90. float student_GPA;
  91.  
  92. cout << "Please enter the GPA: ";
  93. cin >> student_GPA;
  94.  
  95. return student_GPA;
  96. }
  97. int get_Year()
  98. {
  99. int student_year;
  100.  
  101. cout << "Please enter the start Year: ";
  102. cin >> student_year;
  103.  
  104. return student_year;
  105. }
  106. void input_new_student(std::string student_names[], float student_GPA[], int student_start_year[], int index, int ramapo_id[])
  107. {
  108. //information generation
  109. srand((unsigned)time(0));
  110. int random_integer = rand();
  111. ramapo_id[index] = random_integer;
  112.  
  113. //information acquisition
  114. student_names[index] = get_Name();
  115. student_GPA[index] = get_GPA();
  116. student_start_year[index] = get_Year();
  117.  
  118. cout << "Student added suuccessfully";
  119. }
  120. void print_all(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, int ramapo_id[])
  121. {
  122. for (int i = 0; i < size; i++) {
  123. cout << student_names[i] << " - " << ramapo_id[i] << endl;
  124. }
  125. }
  126. void print_by_year(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size)
  127. {
  128. int student_year_identifier;
  129. cout << "Which year would you like to display?: ";
  130. cin >> student_year_identifier;
  131. for (int i = 0; i < size; i++)
  132. {
  133. if (student_year_identifier == student_start_year[i])
  134. {
  135. cout << "There were " << index << "students in that year" << endl;
  136. }
  137. else {
  138. cout << "There were no students in that year" << endl;
  139. }
  140. }
  141. }
  142. void print_statistics(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, float sum)
  143. {
  144. cout << "Total: " << index << endl;
  145.  
  146. float avg = 0.0;
  147. for (int i = 0; i < size; ++i)
  148. {
  149. sum += student_GPA[i];
  150. }
  151. avg = ((float)sum)/size;
  152. cout << "GPA: " << avg << endl;
  153. }
Success #stdin #stdout 0s 16072KB
stdin
1
anil
8.0
2010
stdout
Please pick from the following menu 
1. Add a new student 
2. Print all students 
3. Print students by year 
4. Print student statistics 
5. Quit
Please enter the student's name: Please enter the GPA: Please enter the start Year: Student added suuccessfully