#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
// struct //////////////////////
struct TStudent {
string name;
int grade[4];
float avg;
};
// end struct //////////////////////
// class //////////////////////
class CGrade {
private:
int amount;
TStudent Student[30];
public:
CGrade();
~CGrade();
bool read();
void display();
void calculateAvg();
void sort();
void write();
void add();
void search();
};
// end class //////////////////////
// methods //////////////////////
CGrade::CGrade() {}
CGrade::~CGrade() {}
bool CGrade::read()
{
fstream File;
int i = 0;
bool success;
File.open("Text_11-17.txt", ios::in);
if (File.good()) {
while (!File.eof()) {
File >> Student[i].name;
for (int k = 0; k < 4; k++) {
File >> Student[i].grade[k];
}
i++;
}
amount = i;
success = true;
}
else {
cout << "File could not be opened";
File.close();
success = false;
}
return success;
}
void CGrade::display()
{
for (int i = 0; i < amount; i++) {
cout << ">>>> Name: " << Student[i].name << endl;
for (int k = 0; k < 4; k++) {
cout << "> " << Student[i].grade[k] << endl;
}
cout << ">> Durchschnitt: " << Student[i].avg << endl
<< endl;
}
}
void CGrade::calculateAvg()
{
float total = 0;
for (int i = 0; i < amount; i++) {
total = 0;
for (int k = 0; k < 4; k++) {
total += Student[i].grade[k];
}
Student[i].avg = total / 4;
}
}
void CGrade::sort()
{
TStudent swap;
bool sorted;
do {
sorted = 1;
for (int i = 0; i < amount - 1; i++) {
if (Student[i].avg > Student[i + 1].avg) {
swap = Student[i];
Student[i] = Student[i + 1];
Student[i + 1] = swap;
sorted = 0;
}
}
} while (sorted == 0);
}
void CGrade::write()
{
fstream File;
File.open("Text_11-17_new.txt", ios::out);
if (File.good()) {
for (int i = 0; i < amount; i++) {
File << ">>>> Student #" << i + 1 << ":\t" << Student[i].name << " <<<<" << endl
<< ">> Grades:\t";
for (int k = 0; k < 4; k++) {
File << Student[i].grade[k] << " ";
}
File << endl
<< "> Average:\t" << Student[i].avg << endl
<< endl;
}
}
else {
cout << "File could not be opened";
File.close();
}
}
void CGrade::add()
{
fstream File;
string name;
int grades[4];
cout << "Name des Schuelers:";
cin >> name;
for (int i = 0; i < 4; i++) {
cout << "Grade #" << i + 1 << ":";
cin >> grades[i];
}
File.open("Text_11-17.txt", ios::out | ios::app);
if (File.good()) {
File << endl
<< name;
for (int i = 0; i < 4; i++) {
File << " " << grades[i];
}
}
else {
cout << "File could not be opened";
File.close();
}
}
void CGrade::search() {
string name;
cout<<"Enter a name to search for:";
cin>>name;
for (int i=0; i<amount; i++) {
if(name == Student[i].name) {
cout<<">> Student found!"<<endl<<endl;
cout << ">>>> Name: " << Student[i].name << endl;
for (int k = 0; k < 4; k++) {
cout << "> " << Student[i].grade[k] << endl;
}
cout << ">> Durchschnitt: " << Student[i].avg << endl
<< endl;
}
else {
cout<<"Student could not be found";
}
}
}
// end methods //////////////////////
// main //////////////////////
main()
{
CGrade Grade;
int choice;
bool continue = true;
if (Grade.read()) {
Grade.calculateAvg();
Grade.sort();
do {
cout<<">>>> Choose an action >>>>"<<endl<<
"1)\tDisplay all students"<<endl<<
"2)\tCreate new file with more student data"<<endl<<
"3)\tAdd a student"<<endl;
cin>>choice;
switch(choice) {
case 1 :a
Grade.display();
break;
case 2 :
Grade.write();
break;
case 3 :
Grade.add();
break;
default:
continue=false;
break;
}
} while (continue==true);
}
}
// end main //////////////////////