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
|
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
struct Student
{
string firstName;
string lastName;
double exam1;
double exam2;
double exam3;
Student(string fn, string ln, double s1, double s2, double s3)
{
lastName = ln;
firstName = fn;
exam1 = s1;
exam2 = s2;
exam3 = s3;
}
};
void newRecord(vector<Student>& );
void displayRecord(vector<Student>& );
void deleteRecord(vector<Student>& );
void scoreAverage(vector<Student>&);
void saveAndExit(vector<Student>&);
int main()
{
vector<Student> students;
int option = 0;
do
{
cout << "----------Menu Options--------------" << endl;
cout << " 1. Enter students name and scores " << endl;
cout << " 2. Display students information " << endl;
cout << " 3. Remove a students information " << endl;
cout << " 4. Display Exam Averages " << endl;
cout << " 5. Save and Exit " << endl;
cout << " Enter your choice 1, 2, 3, 4 ,5 " << endl;
cin >> option;
cout << endl << endl;
switch(option)
{
case 1:
newRecord(students);
break;
case 2:
displayRecord(students);
break;
case 3:
deleteRecord(students);
break;
case 4:
scoreAverage(students);
break;
case 5:
saveAndExit(students);
return 0;
default:
cout << "Please enter 1, 2, 3, 4, or 5" << endl << endl;
break;
}
}while(option != 4);
}
void newRecord(vector<Student>& students)
{
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);
students.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 displayRecord(vector<Student>& students)
{
cout << " There are " << students.size() << " at this time. " << endl << endl;
if(students.size() > 0)
{
cout << " RecNum\tFName\tLName\tScore1\tScore2\tScore3\t" << endl;
for(int i = 0; i < students.size(); i++)
cout << " " << (i + 1) << "." << '\t' << students[i].firstName
<< '\t' << students[i].lastName
<< '\t' << students[i].exam1
<< '\t' << students[i].exam2
<< '\t' << students[i].exam3 << endl;
}
cout << endl;
}
void deleteRecord(vector<Student>& students)
{
int index = 0;
cout << "There are " << students.size() << " records at this time. " ;
if(students.size() == 0)
{
cout << " No records can be removed. " << endl << endl;
return;
}
else
{
cout << "The names are: " << endl << endl;
for(int i = 0; i < students.size(); i++)
cout << i + 1 << '\t' << students[i].firstName << " " << students[i].lastName << endl;
cout << endl << " Enter the number of the file that you wish to delete: 1 through " << students.size();
cin >> index;
if(index <= 0 || index > students.size())
{
cout << " The number you entered is not within the proper range." << endl;
return;
}
index = index - 1;
for(int i = index; i < students.size() - 1; i++)
students[i] = students[i+1];
students.pop_back();
}
cout << " There are now " << students.size() << " records." << endl;
}
void scoreAverage(vector<Student> &students)
{
double total = 0;
for (int i = 0; i < students.size(); i++)
total += (students[i].exam1 + students[i].exam2 + students[i].exam3)/3.0;
cout << total * 1.0 / students.size() << endl;
}
void saveAndExit(vector<Student>& students)
{
ofstream outFile;
outFile.open("studentScores.txt");
if(outFile.is_open())
{
for(int i = 0; i < students.size(); i++)
outFile << students[i].firstName << '\t'
<< students[i].lastName << '\t'
<< students[i].exam1 << '\t'
<< students[i].exam2 << '\t'
<< students[i].exam3 << endl;
cout << students.size() << " The records were saved to studentScores.txt" << endl << endl;
}
}
|