Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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:
C++
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;
}
Posted
Updated 27-Apr-22 23:20pm
v2
Comments
Richard MacCutchan 26-Apr-22 14:29pm    
Read all the student details and calculate sum and average from the values that are input.
Jonson123 2022 26-Apr-22 15:48pm    
I did, but the grade point average always reflects the last student who entered the file
Richard MacCutchan 26-Apr-22 15:57pm    
Look at the loop that reads the student data. Each new record overwrites the value of low. So that reflects the last student's details. Maybe you meant to return low_mark from that function.
Jonson123 2022 26-Apr-22 16:04pm    
When I return low_mark it is an error
Low in order to find the lowest average score among all students
Richard MacCutchan 26-Apr-22 16:18pm    
What error? Please be explicit, we can only work with the details you provide.

1 solution

Only one while loop is needed in the LowMark() function. The variables low and low_mark should be initialized with the highest possible value. With the if query you should then remember the lowest value.
 
Share this answer
 
Comments
Jonson123 2022 28-Apr-22 5:21am    
Thank you, i solved the problem (update my code in post)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900