C++ Schol

 avatar
s7s
plain_text
25 days ago
9.7 kB
0
Indexable
Never
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cctype>

using namespace std;

struct stFinalResult
{
	float GradesSum;
	float GradesAverage;
	string Result;
};

struct stGrades
{
	float NumberOfSubjects = 6;
	float HolyQuran;
	float Arabic;
	float English;
	float Math;
	float Physics;
	float Chemistry;
};

struct stStudent
{
	short StudentNo;
	bool Status;
	string FirstName;
	string LastName;
	stGrades Grades;
	stFinalResult FinalResult;
};

float ReadNumberInRange(string Message, short From, short To)
{
	float Number = 0;

	do
	{
		cout << Message;

		cin >> Number;

		cout << endl;

		while (cin.fail())
		{
			cin.clear();

			cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

			cout << " Invalid Input, Try Again \n";

			cout << Message;

			cin >> Number;

			cout << endl;
		}

	} while (Number < From || Number > To);

	return Number;
}

bool PassOrFail(float Mark)
{
	return (Mark >= 50 ? "Pass" : "Fail");
}

string GetPassOrFail(float Mark)
{
	return (Mark >= 50 ? "Pass" : "Fail");
}

float AverageOfGrades(stStudent Student)
{
	return (float)Student.FinalResult.GradesSum / Student.Grades.NumberOfSubjects;
}

float SumOfGrades(stGrades Grades)
{
	return (Grades.HolyQuran + Grades.Arabic + Grades.English + Grades.Math + Grades.Physics + Grades.Chemistry);
}

stStudent ReadStudent(short StudentNo)
{
	stStudent Student;

	Student.StudentNo = StudentNo;

	cout << "Enter First Name ? ";
	cin >> Student.FirstName;

	cout << endl;

	cout << "Enter Last Name ? ";
	cin >> Student.LastName;

	cout << endl;

	Student.Status = true;

	Student.Grades.HolyQuran = ReadNumberInRange("Enter Holy Quran Grade %", 0, 100);

	Student.Grades.Arabic = ReadNumberInRange("Enter Arabic Language Grade %", 0, 100);

	Student.Grades.English = ReadNumberInRange("Enter English Language Grade %", 0, 100);

	Student.Grades.Math = ReadNumberInRange("Enter Math Grade %", 0, 100);

	Student.Grades.Physics = ReadNumberInRange("Enter Physics Grade %", 0, 100);

	Student.Grades.Chemistry = ReadNumberInRange("Enter Chemistry Grade %", 0, 100);

	Student.FinalResult.GradesSum = SumOfGrades(Student.Grades);

	Student.FinalResult.GradesAverage = AverageOfGrades(Student);

	Student.FinalResult.Result = GetPassOrFail(Student.FinalResult.GradesAverage);

	return Student;
}

void AddStudent(stStudent *Students, short &TotalStudents)
{
	TotalStudents++;

	*(Students + (TotalStudents - 1)) = ReadStudent(TotalStudents);
}

void PrintStudent(stStudent Student)
{
	cout << "\n |------------------|---|-------------|------|------|\n";

	cout << " |       Name       | No|   Subject   | Mark |Status|\n";

	cout << " |------------------|---|-------------|------|------|\n";

	cout << " |" << setw(18) << Student.FirstName + " " + Student.LastName << "|" << setw(3) << Student.StudentNo << "|" << setw(13) << "Holy Quran"
		 << "|" << setw(6) << Student.Grades.HolyQuran << "|" << setw(6) << GetPassOrFail(Student.Grades.HolyQuran) << "|\n";

	cout << " |" << setw(18) << " "
		 << "|" << setw(3) << " "
		 << "|" << setw(13) << "Arabic"
		 << "|" << setw(6) << Student.Grades.Arabic << "|" << setw(6) << GetPassOrFail(Student.Grades.Arabic) << "|\n";

	cout << " |" << setw(18) << " "
		 << "|" << setw(3) << " "
		 << "|" << setw(13) << "English"
		 << "|" << setw(6) << Student.Grades.English << "|" << setw(6) << GetPassOrFail(Student.Grades.English) << "|\n";

	cout << " |" << setw(18) << " "
		 << "|" << setw(3) << " "
		 << "|" << setw(13) << "Math"
		 << "|" << setw(6) << Student.Grades.Math << "|" << setw(6) << GetPassOrFail(Student.Grades.Math) << "|\n";

	cout << " |" << setw(18) << " "
		 << "|" << setw(3) << " "
		 << "|" << setw(13) << "Physics"
		 << "|" << setw(6) << Student.Grades.Physics << "|" << setw(6) << GetPassOrFail(Student.Grades.Physics) << "|\n";

	cout << " |" << setw(18) << ""
		 << "|" << setw(3) << ""
		 << "|" << setw(13) << "Chemistry"
		 << "|" << setw(6) << Student.Grades.Chemistry << "|" << setw(6) << GetPassOrFail(Student.Grades.Chemistry) << "|\n";

	cout << " |--------------------------------------------------|\n";

	cout << " |     Sum     |   Average   |      Final Result    |\n";

	cout << " |-------------|-------------|----------------------|\n";

	cout << " |" << setw(13) << Student.FinalResult.GradesSum << "|" << setw(13) << Student.FinalResult.GradesAverage << "|" << setw(22) << GetPassOrFail(Student.FinalResult.GradesAverage) << "|\n";

	cout << " |-------------|-------------|----------------------|\n";
}

bool IsStudentNameInList(string FirstName, string LastName, stStudent Students)
{
	return (Students.FirstName == FirstName && Students.LastName == LastName);
}

void FindAndPrintStudentByName(string FirstName, string LastName, stStudent *Students, short &TotalStudents)
{
	for (short i = 0; i < TotalStudents; i++)
	{
		if (IsStudentNameInList(FirstName, LastName, *(Students + i)))
		{
			PrintStudent(*(Students + i));
			return;
		}
	}

	cout << "\n Sorry, The Student Was Not Found\n";
}

void PrintSearchForStudentMenu()
{
	cout << "\n\n |--------------------------------------------------|\n";

	cout << " |\t       "
		 << "Search For a Student Menu "
		 << "\t    |\n";

	cout << " |--------------------------------------------------| \n";

	cout << " |1-Search By Name  |2-Search By ID  |3-Back Menu   |\n";

	cout << " |--------------------------------------------------|\n\n";
}

void PrintAllStudentsMenu()
{
	cout << "\n |--------------------------------------------------| \n";

	cout << " |\t\t    "
		 << "All Student Menu "
		 << "\t\t    |\n";

	cout << " |--------------------------------------------------| \n";
}

void SearchMenu(stStudent *Students, short &TotalStudents)
{
	if (TotalStudents > 0)
	{
		short Input = 0;

		short *StudentNo;

		string *FirstName, *LastName;

		PrintSearchForStudentMenu();

		Input = ReadNumberInRange(" Please Enter a Number ? ", 1, 3);

		while (Input != 3)
		{
			if (Input == 1)
			{
				FirstName = new string;
				LastName = new string;

				cout << " Please Enter Student First Name ? ";
				cin >> *(FirstName);

				cout << " Please Enter Student Last Name ? ";
				cin >> *(LastName);

				FindAndPrintStudentByName(*(FirstName), *(LastName), Students, TotalStudents);

				delete FirstName;
				delete LastName;
			}

			else if (Input == 2)
			{
				StudentNo = new short;

				*(StudentNo) = ReadNumberInRange(" Please Enter Student Number ? ", 1, 32000);

				if (*(StudentNo) > 0 && *(StudentNo) <= TotalStudents)
				{
					PrintStudent(*(Students + (*(StudentNo)-1)));
				}
				
				else
				{
					cout << "\n Sorry, The Student Was Not Found\n";
				}

				delete StudentNo;
			}

			else
			{
				delete StudentNo;
				delete FirstName;
				delete LastName;
				return;
			}

			PrintSearchForStudentMenu();

			Input = ReadNumberInRange(" Please Enter a Number ? ", 1, 3);
		}
	}
	
	else
	{
		cout << "\n Sorry, There Are No Students !\n";
		return;
	}
}

void PrintStudentMenu()
{
	cout << "\n |--------------------------------------------------| \n";

	cout << " |\t\t    "
		 << "Student Menu "
		 << "\t\t    |\n";

	cout << " |--------------------------------------------------| \n";

	cout << " |"
		 << "1-Add Student";

	cout << " |"
		 << "2-Show All Students  |";

	cout << "3-Back Menu  |\n";

	cout << " |--------------------------------------------------| \n\n";
}

void AllStudentsMenu(stStudent *Student, short &TotalStudents)
{
	system("cls");

	PrintAllStudentsMenu();

	if (TotalStudents > 0)
	{
		for (short i = 0; i < TotalStudents; i++)
		{
			PrintStudent(*(Student + i));
		}
	}

	else
	{
		cout << "\n Sorry, There Are No Students !\n";
	}
}

void StudentsMenu(stStudent *Students, short &TotalStudents)
{
	system("cls");

	PrintStudentMenu();

	short Input = 1;

	Input = ReadNumberInRange("Please Enter a Number ? ", 1, 3);

	while (Input != 3)
	{
		if (Input == 1)
		{
			AddStudent(Students, TotalStudents);
		}

		else if (Input == 2)
		{
			AllStudentsMenu(Students, TotalStudents);
		}

		else
		{
			return;
		}

		PrintStudentMenu();

		Input = ReadNumberInRange("Please Enter a Number ? ", 1, 3);
	}
}

void PrintGoodBye()
{
	cout << "\n |--------------------------------------------------| \n";

	cout << " |\t\t     "
		 << "Good Bye -:)"
		 << "\t\t    |\n";

	cout << " |--------------------------------------------------| \n";
}

void PrintMainMenu()
{
	cout << "\n |--------------------------------------------------| \n";

	cout << " |\t\t       "
		 << "Main Menu "
		 << "\t\t    |\n";

	cout << " |--------------------------------------------------| \n";

	cout << " |"
		 << "1-Students";

	cout << "   |"
		 << "2-Search";

	cout << "   |"
		 << "3-Management  |";

	cout << "4-Exit   |\n";

	cout << " |--------------------------------------------------| \n\n";
}

void PrintOpeningWelcome()
{
	cout << "\n\t  ----------------------------------\n";

	cout << "\t | Welcome To School Management App | \n";

	cout << "\t  ----------------------------------\n\n";
}

void RunSchoolManagement()
{
	short TotalStudents = 0;

	stStudent *Students;

	Students = new stStudent[TotalStudents];

	short Input = 0;

	PrintOpeningWelcome();

	PrintMainMenu();

	while (Input != 4)
	{
		Input = ReadNumberInRange(" Enter a Number ? ", 1, 4);

		if (Input == 1)
		{
			system("cls");
			
			StudentsMenu(Students, TotalStudents);
		}

		else if (Input == 2)
		{
			system("cls");
			
			SearchMenu(Students, TotalStudents);
		}

		else if (Input == 3)
		{
		}

		else
		{
			delete[] Students;

			system("cls");

			PrintGoodBye();

			break;
		}
		
		PrintMainMenu();
	}
}

int main(int argc, char *argv[])
{
	RunSchoolManagement();

	return 0;
}