Very Simple and Efficient Student Database
Submitted by DoctorSpeedCode on Friday, August 8, 2014 - 05:06.
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:
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:
As a trick, while reading the marks, we also compute the average for the students.
Then, we display the marks for each course:
The last thing is that we display student averages:
This was simple, right? The best thing is that you can improve it to infinity! The full listing for main.cpp is here:
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!
- #include <iostream>
- #define N 5
- #define COURSES 3
- using namespace std;
- typedef struct x {
- string fullName;
- int marks[COURSES];
- float average;
- } Student;
- string Courses[COURSES] = {"Biology", "Mathematics", "Chemistry"};
- Student MyClass[N];
- for (i = 0; i < N; i++) {
- cout << "Student " << i + 1 << endl;
- cout << "Full name: ";
- getline(cin, MyClass[i].fullName);
- //cin >> ;
- for (j = 0; j < COURSES; j++) {
- cout << "Mark (" << Courses[j] << "): ";
- cin >> MyClass[i].marks[j];
- MyClass[i].average += MyClass[i].marks[j];
- }
- MyClass[i].average /= (float) COURSES;
- cin.ignore(100, '\n');
- }
- for (i = 0; i < COURSES; i++) {
- cout << Courses[i] << " marks:" << endl;
- for (j = 0; j < N; j++) {
- cout << MyClass[j].fullName << " - " << MyClass[j].marks[i] << endl;
- }
- }
- for (j = 0; j < N; j++) {
- cout << MyClass[j].fullName << " - " << MyClass[j].average << endl;
- }
- #include <iostream>
- #define N 5
- #define COURSES 3
- using namespace std;
- typedef struct x {
- string fullName;
- int marks[COURSES];
- float average;
- } Student;
- string Courses[COURSES] = {"Biology", "Mathematics", "Chemistry"};
- Student MyClass[N];
- int main(int argc, char** argv) {
- int i, j;
- for (i = 0; i < N; i++) {
- cout << "Student " << i + 1 << endl;
- cout << "Full name: ";
- getline(cin, MyClass[i].fullName);
- //cin >> ;
- for (j = 0; j < COURSES; j++) {
- cout << "Mark (" << Courses[j] << "): ";
- cin >> MyClass[i].marks[j];
- MyClass[i].average += MyClass[i].marks[j];
- }
- MyClass[i].average /= (float) COURSES;
- cin.ignore(100, '\n');
- }
- cout << endl << endl;
- for (i = 0; i < COURSES; i++) {
- cout << Courses[i] << " marks:" << endl;
- for (j = 0; j < N; j++) {
- cout << MyClass[j].fullName << " - " << MyClass[j].marks[i] << endl;
- }
- }
- cout << endl << "Student averages:" << endl;
- for (j = 0; j < N; j++) {
- cout << MyClass[j].fullName << " - " << MyClass[j].average << endl;
- }
- return 0;
- }
Add new comment
- 54 views