fork download
  1. #include <iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class student
  8. {
  9. private:
  10. string studentName;
  11. double studentGpa;
  12. int studentRank, studentYear;
  13.  
  14. public:
  15. void setName(string name)
  16. {
  17. studentName = name;
  18. }
  19. void setGpa(double gpa)
  20. {
  21. studentGpa = gpa;
  22. }
  23. void setRank(int rank)
  24. {
  25. studentRank = rank;
  26. }
  27. void setYear(int year)
  28. {
  29. studentYear = year;
  30. }
  31. string getName() const
  32. {
  33. return studentName;
  34. }
  35. double getGpa() const
  36. {
  37. return studentGpa;
  38. }
  39. int getRank() const
  40. {
  41. return studentRank;
  42. }
  43. int getYear() const
  44. {
  45. return studentYear;
  46. }
  47. };
  48.  
  49.  
  50. void fillVector(vector<student>&);
  51. double getHighestGpa(const vector<student>&);
  52. void printHighestGpa(const vector<student>&, const double& hiGpa);
  53.  
  54.  
  55. int main()
  56. {
  57. vector<student> allStudents;
  58.  
  59. fillVector(allStudents);
  60.  
  61. double hiGpa = getHighestGpa(allStudents);
  62.  
  63. printHighestGpa(allStudents, hiGpa);
  64.  
  65. return 0;
  66. }
  67.  
  68.  
  69. void fillVector(vector<student> & newAllStudents)
  70. {
  71. string name;
  72. double gpa;
  73. int year, rank;
  74.  
  75. cout << "How many students are there: " << endl;
  76. int numStudents;
  77. cin >> numStudents;
  78.  
  79. for (int i = 0; i < numStudents; i++)
  80. {
  81. cout << "Enter student name: " << endl;
  82. cin >> name;
  83. cout << "Enter student GPA: " << endl;
  84. cin >> gpa;
  85. cout << "Enter student year: " << endl;
  86. cin >> year;
  87. cout << "Enter student rank: " << endl;
  88. cin >> rank;
  89.  
  90. student newStudent;
  91. newStudent.setName(name);
  92. newStudent.setGpa(gpa);
  93. newStudent.setYear(year);
  94. newStudent.setRank(rank);
  95. newAllStudents.push_back(newStudent);
  96. }
  97.  
  98. }
  99.  
  100.  
  101. double getHighestGpa(const vector<student>& newAllStudents)
  102. {
  103. unsigned int size = newAllStudents.size();
  104. double highestGpa = 0;
  105.  
  106. for (unsigned int i = 0; i < size; i++)
  107. {
  108. if (newAllStudents[i].getGpa() > highestGpa)
  109. {
  110. highestGpa = newAllStudents[i].getGpa();
  111. }
  112.  
  113. }
  114.  
  115. return highestGpa;
  116. }
  117.  
  118.  
  119. void printHighestGpa(const vector<student>& newAllStudents,
  120. const double& hiGpa)
  121. {
  122. unsigned int size = newAllStudents.size();
  123. double highestGpa = 0;
  124.  
  125. cout << "The student(s) who won the scholarship is/are: " << endl;
  126.  
  127. for (unsigned int i = 0; i < size; i++)
  128. {
  129. if (newAllStudents[i].getGpa() == hiGpa)
  130. {
  131. cout << "Student name: " << newAllStudents[i].getName() << endl;
  132. cout << "Student GPA: " << hiGpa << endl;
  133. cout << "Student year and rank: "
  134. << newAllStudents[i].getYear() << "("
  135. << newAllStudents[i].getRank() << ")"
  136. << endl << endl;
  137. }
  138. }
  139. }
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
How many students are there: 
The student(s) who won the scholarship is/are: