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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;
class Student { // keep this class simple and to a minimum
public:
Student(string firstN, string lastN, double ex1, double ex2, double ex3);
string getFirstN();
string getLastN();
double getEx1();
double getEx2();
double getEx3();
private:
string firstN;
string lastN;
double ex1;
double ex2;
double ex3;
};
Student::Student(string fn, string ln, double e1, double e2, double e3) {
firstN = fn;
lastN = ln;
ex1 = e1;
ex2 = e2;
ex3 = e3;
}
string Student::getFirstN() {
return firstN;
}
string Student::getLastN() {
return lastN;
}
double Student::getEx1() {
return ex1;
}
double Student::getEx2() {
return ex2;
}
double Student::getEx3() {
return ex3;
}
class StudentData{ // this class manages the MileageEntries
public:
StudentData();
void loadDataFromFile();
void addStudent();
void deleteStudent();
void displayStudentData();
void displayAverageScore();
void saveAndExit();
private:
vector<Student> entries; // vector of Student objects
};
StudentData::StudentData() {} // default constructor
void StudentData::loadDataFromFile() {
string firstN, lastN;
double ex1, ex2, ex3;
ifstream studentData;
studentData.open("studentData.txt"); // open the file
if(studentData.is_open()) {
while( studentData >> firstN >> lastN >> ex1 >> ex2 >> ex3 ) {
Student student(firstN, lastN, ex1, ex2, ex3); // create aStudent object
entries.push_back(student);
}
}
cout << entries.size() << " Total students entered "<< endl << endl;
studentData.close();
}
void StudentData::addStudent() {
string fn, ln;
double s1, s2, s3;
char ans;
do
{
cout << " Enter First Name: ";
cin >> fn;
cout << " Enter Last Name: ";
cin >> ln;
cout << " Enter Score 1: ";
cin >> s1;
cout << " Enter Score 2: ";
cin >> s2;
cout << " Enter Score 3: ";
cin >> s3;
Student temp(fn,ln,s1,s2,s3);
entries.push_back(temp);
cout << " Would you like to enter another record? Y or N : ";
cin >> ans;
ans = tolower(ans);
cout << endl << endl;
}while(ans == 'y');
}
void StudentData::deleteStudent();
{
int index = 0;
cout << "There are " << entries.size() << " records at this time. " ;
if(entries.size() == 0)
{
cout << " No records can be removed. " << endl << endl;
return;
}
else
{
cout << "The names are: " << endl << endl;
for(int i = 0; i < entries.size(); i++)
cout << i + 1 << '\t' << entries[i].firstN << " " << entries[i].lastN << endl;
cout << endl << " Enter the number of the file that you wish to delete: 1 through " << students.size();
cin >> index;
if(index <= 0 || index > entries.size())
{
cout << " The number you entered is not within the proper range." << endl;
}
index = index - 1;
for(int i = index; i < entries.size() - 1; i++)
entries[i] = entries[i+1];
entries.pop_back();
}
cout << " There are now " << entries.size() << " records." << endl;
}
void StudentData::displayAverageScore() {
double totalEx1 = 0, totalEx2 = 0, totalEx3 = 0, averageScore = 0;
for(int i = 0; i < student.size(); i++) {
totalEx1 += entries[i].getEx1();// accumulate totals
totalEx2 += entries[i].getEx2();
totalEx3 += entries[i].getEx3();
}
if(totalScore != 0) averageScore = totalEx1 + totalEx2 + totalEx3 / totalScore; // don't divide by 0
cout << "Average Score For Exams: " << setprecision(1) << fixed << averageScore << endl << endl;
}
void StudentData::saveAndExit() {
ofstream outFile;
outFile.open("studentData.txt");
if(outFile.is_open())
{
for(int i = 0; i < entries.size(); i++)
outFile << entries[i].firstName << '\t'
<< entries[i].lastName << '\t'
<< entries[i].exam1 << '\t'
<< entries[i].exam2 << '\t'
<< entries[i].exam3 << endl;
cout << entries.size() << " The records were saved to studentData.txt" << endl << endl;
}
}// do later
int main() { // test program
StudentData entries; //
int option;
entries.loadDataFromFile(); // load from file; assumes a pre-existing file
do {
cout << "----------Menu Options--------------" << endl;
cout << " 1. Load a students records " << endl;
cout << " 2. Enter students name and scores " << endl;
cout << " 3. Display students information " << endl;
cout << " 4. Remove a students information " << endl;
cout << " 5. Display Exam Averages " << endl;
cout << " 6. Save and Exit " << endl;
cout << " Enter your choice 1, 2, 3, 4 ,5 " << endl;
cin >> option;
cin.clear();
cin.ignore(50, '\n');
cout << endl;
switch(option) {
case 1:
mLog.loadDataFromFile(); // add an entry object to the log
break;
case 2:
mLog.addStudent(); // add an entry object to the log
break;
case 3:
mLog.displayStudentData(); // display all records
break;
case 4:
mLog.deleteStudent();
break;
case 5:
mLog.displayAverageScore(); // display average MPG
break;
case 6:
mLog.saveAndExit(); // save to file and exit program
break;
default:
cout << "Please enter 1, 2, 3, 4, 5, or 6 : " << endl << endl;
}
} while(option!= 6);
cout << endl;
return 0;
}
|