-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
172 lines (137 loc) · 4.26 KB
/
database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* A database source file that is able to read or write student's information in a text file
* database.cpp
*
* Created on: 02 Mar 2018
* Author: Tumelo
*/
#include "database.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
using namespace std;
std::vector<student_record> students;
/*
* add_student() is a void method which adds students to the databse
*/
void lphtum003::add_student(std::string name, std::string surname, std::string student_number, std::string class_record){
bool existing_student = false;
for(unsigned int i = 0; i < students.size(); i++){
if (students[i].student_number.compare(student_number) == 0){
existing_student = true;
students[i].name = name;
students[i].surname = surname;
students[i].student_number = student_number;
students[i].class_record = class_record;
break;
}
}
if(!existing_student){
student_record new_student;
new_student.name = name;
new_student.surname = surname;
new_student.student_number = student_number;
new_student.class_record = class_record;
students.push_back(new_student);
}
}
/*
* read_database() is a void method that is able to read student information from a text file and save it into the database
*/
void lphtum003::read_database(std::string filename){
cout << "Enter filename:" << endl;
cin >> filename;
// Open file with a file stream. ifstream constructor
// wants a C char * string, so call c_str() method.
ifstream in(filename.c_str());
if(!in){
cout << "Couldn't open " << filename << endl;
in.close();
}
else{
while(!in.eof()){
string sentence, data_entry;
std::vector<string> lines;
student_record new_student;
getline(in, sentence);
std::istringstream iss(sentence);
while(std::getline(iss, data_entry, ' ')){
lines.push_back(data_entry);
}
for(unsigned int i = 0; i < 3; i++){
new_student.name = lines[0];
new_student.surname = lines[1];
new_student.student_number = lines[2];
}
for (unsigned int j = 3; j < lines.size(); j++)
{
new_student.class_record = new_student.class_record + lines[j] + " ";
}
lphtum003::add_student(new_student.name, new_student.surname, new_student.student_number, new_student.class_record);
}
}
}
/*
* save_database is a void method that writes all the information from the database into a textfile
*/
void lphtum003::save_database(std::string filename){
cout << "Enter filename:" << endl;
cin >> filename;
ofstream ofs(filename.c_str());
for (unsigned int i = 0; i < students.size(); i++)
{
ofs << students[i].name << " " << students[i].surname << " " << students[i].student_number << " " << students[i].class_record;
ofs << endl;
}
}
/*
* display_student is a void method that writes the information of a student with the given student number onto the console
*/
void lphtum003::display_student(std::string student_number){
cout << "Enter student number:" << endl;
cin >> student_number;
bool existing_student = false;
for(unsigned int i = 0; i < students.size(); i++){
if(students[i].student_number.compare(student_number) == 0){
existing_student = true;
cout << students[i].name << " ";
cout << students[i].surname << " ";
cout << students[i].student_number << " ";
cout << students[i].class_record << endl;
}
}
if(!existing_student){
cout << "Student not found." << endl;
}
}
/*
* grade_student() is a void method that writes the grade average of a student with the given student number onto the console
*/
void lphtum003::grade_student(std::string student_number){
cout << "Enter student number:" << endl;
cin >> student_number;
bool existing_student = false;
std::vector<string> grades;
std::string grade;
unsigned int sum = 0;
float average = 0.0;
for(unsigned int i = 0; i < students.size(); i++){
if (students[i].student_number.compare(student_number) == 0){
existing_student = true;
std::stringstream ss(students[i].class_record);
while(std::getline(ss, grade, ' ')){
grades.push_back(grade);
}
}
}
if(existing_student){
for(unsigned int i = 0; i < grades.size(); i++){
sum = stoi(grades[i]) + sum;
}
average = sum / static_cast<double>(grades.size());
printf("The average is: %3.2f \n", average);
}
else{
cout << "Student not found." << endl;
}
}