I need to find the lowest average score among all the students in the file.
I was looking for a grade point average, but this is a search for the last student whose was entered at file, how do I look for a grade point average among all students?
What I have tried:
My code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
enum Specialty { KOMP_NAYK, INFORMATUKA, MATEM_EKONOM, FIZ_MATEM, TRUD_NAVCH };
string specialtyStr[] = { "KOMP_NAYK", "INFORMATUKA", "MATEM_EKONOM", "FIZ_MATEM", "TRUD_NAVCH " };
struct Student
{
char surname[64];
unsigned course;
Specialty specialty;
unsigned physics;
unsigned maths;
union
{
unsigned programming;
unsigned ch_methods;
unsigned pedagogy;
};
};
void CreateBIN(const char* filename);
void PrintBIN(const char* filename);
double LowMark(const char* filename);
int main()
{
char filename[100] = "";
int menuItem;
do {
cout << endl;
cout << " Choose an action: " << endl << endl;
cout << " [1] - data entry from the keyboard " << endl;
cout << " [2] - output to the screen " << endl;
cout << " [0] - exit and end of the program " << endl << endl;
cout << " Enter a value: ";
cin >> menuItem;
cout << endl;
switch (menuItem)
{
case 1:
if (strcmp(filename, "") == 0) {
cout << "File name: ";
cin >> filename;
}
CreateBIN(filename);
break;
case 2:
PrintBIN(filename);
cout << " The lowest average score: " << LowMark(filename) << endl;
break;
case 0:
break;
default:
cout << " You entered an incorrect value! "
" You must enter the number - the number of the selected menu item " << endl;
}
} while (menuItem != 0);
return 0;
}
void CreateBIN(const char* filename)
{
ofstream f(filename, ios::binary);
char ch;
int n = 1;
int specialty;
Student student;
do {
cout << " Student № " << n << ":" << endl;
cin.get();
cin.sync();
cout << " Surname: ";
cin >> student.surname;
cout << " Course: ";
cin >> student.course;
cout << " Specialty (0 - KOMP_NAYK, 1 - INFORMATUKA, 2 - MATEM_EKONOM, 3 - FIZ_MATEM, 4 - TRUD_NAVCH): ";
cin >> specialty;
student.specialty = (Specialty)specialty;
cout << " mark physics: ";
cin >> student.physics;
cout << " mark mathematics: ";
cin >> student.maths;
switch (student.specialty)
{
case KOMP_NAYK:
cout << " mark programming: ";
cin >> student.programming;
break;
case INFORMATUKA:
cout << " mark numerical method: ";
cin >> student.ch_methods;
break;
case MATEM_EKONOM:
case FIZ_MATEM:
case TRUD_NAVCH:
cout << " mark pedagogy: ";
cin >> student.pedagogy;
break;
}
cout << endl;
f.write((char*)&student, sizeof(Student));
n++;
cout << "Add more? (y/n): ";
cin >> ch;
cout << endl;
}
while (ch == 'y' || ch == 'Y');
cout << endl;
f.close();
}
void PrintBIN(const char* filename)
{
ifstream f(filename, ios::binary);
cout << "=================================================================================================================================" << endl;
cout << " | № | Surname | Course | Specialty | Physics | Maths | Programming | Num_Methods | Pedagogy |" << endl;
cout << "---------------------------------------------------------------------------------------------------------------------------------" << endl;
Student student;
int i = 1;
while (f.read((char*)&student, sizeof(Student))) {
cout << " | " << setw(2) << right << i << " ";
cout << " | " << setw(14) << left << student.surname;
cout << " | " << setw(2) << right << student.course << " ";
cout << " | " << setw(23) << left << specialtyStr[student.specialty];
cout << " | " << setw(4) << right << student.physics << " ";
cout << " | " << setw(6) << right << student.maths << " ";
switch (student.specialty)
{
case KOMP_NAYK:
cout << " | " << setw(7) << right << student.programming << " " << " | " << setw(18) << right << " | " << setw(12) << right << "|" << endl;
break;
case INFORMATUKA:
cout << " | " << setw(16) << right << "| " << setw(8) << right << student.ch_methods << " " << " | " << setw(12) << right << "|" << endl;
break;
case MATEM_EKONOM:
case FIZ_MATEM:
case TRUD_NAVCH:
cout << " | " << setw(16) << right << "| " << setw(18) << right << " | " << setw(6) << right << student.pedagogy << " " << "|" << endl;
break;
}
i++;
}
cout << "=================================================================================================================================" << endl;
cout << endl;
}
double LowMark(const char* filename)
{
ifstream f(filename, ios::binary);
Student student;
double low;
double low_mark;
while (f.read((char*)&student, sizeof(Student)))
{
switch (student.specialty)
{
case KOMP_NAYK:
low = (student.physics + student.maths + student.programming) / 3.0;
break;
case INFORMATUKA:
low = (student.physics + student.maths + student.ch_methods) / 3.0;
break;
case MATEM_EKONOM:
case FIZ_MATEM:
case TRUD_NAVCH:
low = (student.physics + student.maths + student.pedagogy) / 3.0;
break;
}
}
while (f.read((char*)&student, sizeof(Student)))
{
switch (student.specialty)
{
case KOMP_NAYK:
low_mark = (student.physics + student.maths + student.programming) / 3.0;
break;
case INFORMATUKA:
low_mark = (student.physics + student.maths + student.ch_methods) / 3.0;
break;
case MATEM_EKONOM:
case FIZ_MATEM:
case TRUD_NAVCH:
low_mark = (student.physics + student.maths + student.pedagogy) / 3.0;
break;
}
if (low_mark < low)
{
low = low_mark;
}
}
f.close();
return low;
}
UPDATE:
double LowMark(const char* filename)
{
ifstream f(filename, ios::binary);
Student student;
double low = 5;
double low_mark = 5;
while (f.read((char*)&student, sizeof(Student)))
{
switch (student.specialty)
{
case KOMP_NAYK:
low_mark = (student.physics + student.maths + student.programming) / 3.0;
break;
case INFORMATUKA:
low_mark = (student.physics + student.maths + student.ch_methods) / 3.0;
break;
case MATEM_EKONOM:
case FIZ_MATEM:
case TRUD_NAVCH:
low_mark = (student.physics + student.maths + student.pedagogy) / 3.0;
break;
}
if (low_mark < low)
{
low = low_mark;
}
}
f.close();
return low;
}