Very Simple and Efficient Student Database

The next project will demonstrate advantages of C++, such as: - using typedef - using the #define directive - using simple, short source code to handle apparently large tasks - easy to read code, if you develop your simple programming style The task we are going to accomplish is a simple and efficient student database. Although we have put some amounts (limitations), these can be changed very easily! Furthermore, for the simplicity of the tutorial we only used cin to read data. However, this can and should be extended to reading from a file. We have used the following declarations:
  1. #include <iostream>
  2.  
  3. #define N 5
  4. #define COURSES 3
  5.  
  6. using namespace std;
  7.  
  8. typedef struct x {
  9. string fullName;
  10. int marks[COURSES];
  11. float average;
  12. } Student;
  13.  
  14. string Courses[COURSES] = {"Biology", "Mathematics", "Chemistry"};
  15. Student MyClass[N];
What we see is: 1) Defining 2 constant values with #define 2) Using typedef to construct a struct element for student data 3) A string array for course names 4) A student array for student data Do note that these can be extended to STL templates, such as or others! The main.cpp file has 3 sections: 1) Reading student data 2) Displaying student marks for each course 3) Displaying student averages The code for reading student data is the following:
  1. for (i = 0; i < N; i++) {
  2. cout << "Student " << i + 1 << endl;
  3. cout << "Full name: ";
  4. getline(cin, MyClass[i].fullName);
  5. //cin >> ;
  6. for (j = 0; j < COURSES; j++) {
  7. cout << "Mark (" << Courses[j] << "): ";
  8. cin >> MyClass[i].marks[j];
  9. MyClass[i].average += MyClass[i].marks[j];
  10. }
  11. MyClass[i].average /= (float) COURSES;
  12. cin.ignore(100, '\n');
  13. }
As a trick, while reading the marks, we also compute the average for the students. Then, we display the marks for each course:
  1. for (i = 0; i < COURSES; i++) {
  2. cout << Courses[i] << " marks:" << endl;
  3. for (j = 0; j < N; j++) {
  4. cout << MyClass[j].fullName << " - " << MyClass[j].marks[i] << endl;
  5. }
  6. }
The last thing is that we display student averages:
  1. for (j = 0; j < N; j++) {
  2. cout << MyClass[j].fullName << " - " << MyClass[j].average << endl;
  3. }
This was simple, right? The best thing is that you can improve it to infinity! The full listing for main.cpp is here:
  1. #include <iostream>
  2.  
  3. #define N 5
  4. #define COURSES 3
  5.  
  6. using namespace std;
  7.  
  8. typedef struct x {
  9. string fullName;
  10. int marks[COURSES];
  11. float average;
  12. } Student;
  13.  
  14. string Courses[COURSES] = {"Biology", "Mathematics", "Chemistry"};
  15. Student MyClass[N];
  16.  
  17. int main(int argc, char** argv) {
  18. int i, j;
  19. for (i = 0; i < N; i++) {
  20. cout << "Student " << i + 1 << endl;
  21. cout << "Full name: ";
  22. getline(cin, MyClass[i].fullName);
  23. //cin >> ;
  24. for (j = 0; j < COURSES; j++) {
  25. cout << "Mark (" << Courses[j] << "): ";
  26. cin >> MyClass[i].marks[j];
  27. MyClass[i].average += MyClass[i].marks[j];
  28. }
  29. MyClass[i].average /= (float) COURSES;
  30. cin.ignore(100, '\n');
  31. }
  32. cout << endl << endl;
  33. for (i = 0; i < COURSES; i++) {
  34. cout << Courses[i] << " marks:" << endl;
  35. for (j = 0; j < N; j++) {
  36. cout << MyClass[j].fullName << " - " << MyClass[j].marks[i] << endl;
  37. }
  38. }
  39. cout << endl << "Student averages:" << endl;
  40. for (j = 0; j < N; j++) {
  41. cout << MyClass[j].fullName << " - " << MyClass[j].average << endl;
  42. }
  43. return 0;
  44. }
A simple output, for random data entered:
  1. Student 1
  2. Full name: Abraham Cole
  3. Mark (Biology): 80
  4. Mark (Mathematics): 74
  5. Mark (Chemistry): 62
  6. Student 2
  7. Full name: Brenda Johnson
  8. Mark (Biology): 100
  9. Mark (Mathematics): 80
  10. Mark (Chemistry): 75
  11. Student 3
  12. Full name: Kate Newark
  13. Mark (Biology): 60
  14. Mark (Mathematics): 70
  15. Mark (Chemistry): 95
  16. Student 4
  17. Full name: John Lewis
  18. Mark (Biology): 50
  19. Mark (Mathematics): 70
  20. Mark (Chemistry): 70
  21. Student 5
  22. Full name: Peter Pan
  23. Mark (Biology): 100
  24. Mark (Mathematics): 100
  25. Mark (Chemistry): 100
  26.  
  27.  
  28. Biology marks:
  29. Abraham Cole - 80
  30. Brenda Johnson - 100
  31. Kate Newark - 60
  32. John Lewis - 50
  33. Peter Pan - 100
  34. Mathematics marks:
  35. Abraham Cole - 74
  36. Brenda Johnson - 80
  37. Kate Newark - 70
  38. John Lewis - 70
  39. Peter Pan - 100
  40. Chemistry marks:
  41. Abraham Cole - 62
  42. Brenda Johnson - 75
  43. Kate Newark - 95
  44. John Lewis - 70
  45. Peter Pan - 100
  46.  
  47. Student averages:
  48. Abraham Cole - 72
  49. Brenda Johnson - 85
  50. Kate Newark - 75
  51. John Lewis - 63.3333
  52. Peter Pan - 100
Enjoy!

Tags

Add new comment