Advertisement
Garey

Students Structs

Nov 17th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. struct Student {
  9.     string faculty_number;
  10.     string name;
  11.     double average_grade;
  12. };
  13.  
  14. void input_students(Student *, size_t);
  15. void output_students(Student *, size_t);
  16. void print(const string&);
  17. void output_sorted(Student*, size_t);
  18. void output_excellent_grade_students(Student *, size_t);
  19.  
  20. int main() {
  21.     // Declare test as pointer of Student structure
  22.     size_t *number_of_students = new size_t;
  23.  
  24.     cout << "Enter number of students: ";
  25.     cin >> *number_of_students;
  26.  
  27.     Student *students = new Student[*number_of_students];
  28.  
  29.     // Call input function
  30.     input_students(students, *number_of_students);
  31.    
  32.     // Call output function
  33.     output_students(students, *number_of_students);
  34.  
  35.     // Sort
  36.     output_sorted(students, *number_of_students);
  37.  
  38.     output_excellent_grade_students(students, *number_of_students);
  39.  
  40.     // Free memory
  41.     delete[] students, number_of_students;
  42.  
  43.     system("pause");
  44.     return 0;
  45. }
  46.  
  47. void input_students(Student *student, size_t number_of_students) {
  48.     for(size_t i = 0; i < number_of_students; i++) {
  49.         // Clear input stream and sync it
  50.         cin.sync();
  51.         cin.clear();
  52.  
  53.         cout << "Enter Student #" << i << " name: ";
  54.         getline(cin, student[i].name);
  55.  
  56.         cout << "Enter Student #" << i << " faculty number: ";
  57.         getline(cin, student[i].faculty_number);
  58.  
  59.         cout << "Enter Student #" << i << " average grade: ";
  60.         cin >> student[i].average_grade;
  61.     }
  62. }
  63.  
  64. void output_students(Student *student, size_t number_of_students) {
  65.     for(size_t i = 0; i < number_of_students; i++)
  66.         cout << "\nStudent with faculty number " << student[i].faculty_number << " whose name is " << student[i].name << " has grade of " << student[i].average_grade << endl << endl;
  67. }
  68.  
  69. void output_sorted(Student* student, size_t number_of_students) {
  70.     set<string> sorted_string;
  71.  
  72.     for(size_t i = 0; i < number_of_students; i++) {
  73.         sorted_string.insert(student[i].name);
  74.     }
  75.  
  76.     for_each(sorted_string.begin(), sorted_string.end(), &print);
  77. }
  78.  
  79. void print(const std::string& item)
  80. {
  81.     cout << item << endl;
  82. }
  83.  
  84. void output_excellent_grade_students(Student *student, size_t number_of_students) {
  85.     for(size_t i = 0; i < number_of_students; i++)
  86.         if(student[i].average_grade >= 5.50)
  87.             cout << "\nStudent with faculty number " << student[i].faculty_number << " whose name is " << student[i].name << " has grade of " << student[i].average_grade << endl << endl;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement