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: Student 1 Full name: Abraham Cole Mark (Biology): 80 Mark (Mathematics): 74 Mark (Chemistry): 62 Student 2 Full name: Brenda Johnson Mark (Biology): 100 Mark (Mathematics): 80 Mark (Chemistry): 75 Student 3 Full name: Kate Newark Mark (Biology): 60 Mark (Mathematics): 70 Mark (Chemistry): 95 Student 4 Full name: John Lewis Mark (Biology): 50 Mark (Mathematics): 70 Mark (Chemistry): 70 Student 5 Full name: Peter Pan Mark (Biology): 100 Mark (Mathematics): 100 Mark (Chemistry): 100 Biology marks: Abraham Cole - 80 Brenda Johnson - 100 Kate Newark - 60 John Lewis - 50 Peter Pan - 100 Mathematics marks: Abraham Cole - 74 Brenda Johnson - 80 Kate Newark - 70 John Lewis - 70 Peter Pan - 100 Chemistry marks: Abraham Cole - 62 Brenda Johnson - 75 Kate Newark - 95 John Lewis - 70 Peter Pan - 100 Student averages: Abraham Cole - 72 Brenda Johnson - 85 Kate Newark - 75 John Lewis - 63.3333 Peter Pan - 100 Enjoy!
Tags

Add new comment