#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
struct Student
{
char id[20];
char name[256];
char course[256];
int year;
double score[3];
double total[3];
double grade[3];
double fgrade;
};
Student studentList[100];
const int SIZE = 100;
int sizeOfArray = 0;
FILE *f;
void AddStudent();
void UpdateStudent();
void DeleteStudent();
void SearchStudent();
void ShowStudentHighestToLowest();
void ShowStudentLowesToHighest();
void ShowStudentInfo(Student student);
void SaveToFile();
void SetStudentData();
void ShowList();
int main()
{
char pick;
int task;
SetStudentData();
while(true)
{
system("CLEAR");
printf("=========================================================");
printf("\n=================Student Records MENU====================");
printf("\n=========================================================");
printf("\n# [1]Input New Student Record\t\t\t\t#");
printf("\n# [2]Modify Student Record\t\t\t\t#");
printf("\n# [3]Delete Student Record\t\t\t\t#");
printf("\n# [4]Show All Existing Student Record\t\t\t#");
printf("\n# [5]Show the information of an specific Student\t#");
printf("\n# [6]Display Student Record[Best To Worst Final Grade]\t#");
printf("\n# [7]Display Student Record[Worst To Best Final Grade]\t#");
printf("\n# [8]Exit\t\t\t\t\t\t#");
printf("\n=========================================================");
printf("\nEnter Task: ");
scanf("%d",&task);
printf("=========================================================\n");
switch(task)
{
case 1:
AddStudent();
SaveToFile();
break;
case 2:
UpdateStudent();
SaveToFile();
break;
case 3:
DeleteStudent();
SaveToFile();
break;
case 4:
ShowList();
break;
case 5:
SearchStudent();
break;
case 6:
ShowStudentHighestToLowest();
break;
case 7:
ShowStudentLowesToHighest ();
break;
case 8:
printf("Thank You For Using The Student Database Program");
return 0;
default:
printf("Invalid Option...\n");
printf("============================================\n");
cin.get();
break;
}
}
}
void AddStudent()
{
int index = 0;
for(int i = 0; i < SIZE; i++)
{
if(strcmp(studentList[i].name, "") == 0)
{
index = i;
break;
}
}
printf("Putting the student in index: %d", index);
printf("\nEnter the data of the student\n");
printf("ID Number: ");
scanf("%s", studentList[index].id);
printf("Name: ");
scanf("%s", studentList[index].name);
printf("Course: ");
scanf("%s", studentList[index].course);
printf("Year Level: ");
scanf("%d", &studentList[index].year);
for(int i = 0; i < 3; i++)
{
printf("Input Score[%d]: ", i + 1);
scanf("%lf", &studentList[index].score[i]);
printf("Input Total Score[%d]: ", i + 1);
scanf("%lf", &studentList[index].total[i]);
studentList[index].grade[i] = 5 - (4 * (studentList[index].score[i]/studentList[index].total[i]));
}
studentList[index].fgrade = studentList[index].grade[0] * 0.2 + studentList[index].grade[1]
* 0.3 + studentList[index].grade[2] * 0.5;
printf("\n\nSuccessfully added the student");
cin.get();
cin.get();
}
void UpdateStudent()
{
char searchID[20];
bool found = false;
char input[1];
while(true)
{
printf("\nEnter the ID number of the student: ");
scanf("%s", searchID);
for(int i = 0; i < sizeOfArray; i++)
{
if(strcmp(studentList[i].id, searchID) == 0)
{
found = true;
printf("Found!\n");
ShowStudentInfo(studentList[i]);
printf("\nEnter the newdata of the student\n");
printf("ID Number: ");
scanf("%s", studentList[i].id);
printf("Name: ");
scanf("%s", studentList[i].name);
printf("Course: ");
scanf("%s", studentList[i].course);
printf("Year Level: ");
scanf("%d", &studentList[i].year);
for(int j = 0; j < 3; j++)
{
printf("Input Score[%d]: ", j + 1);
scanf("%lf", &studentList[i].score[j]);
printf("Input Total Score[%d]: ", j + 1);
scanf("%lf", &studentList[i].total[j]);
studentList[i].grade[j] = 5 - (4 * (studentList[i].score[j]/studentList[i].total[j]));
}
studentList[i].fgrade = studentList[i].grade[0] * 0.2 + studentList[i].grade[1]
* 0.3 + studentList[i].grade[2] * 0.5;
printf("\n\nSuccessfully updated the student");
cin.get();
cin.get();
}
}
if(!found)
{
printf("Student not found, try again? [Y/N]: ");
scanf("%s", input);
if(strcmp(input, "Y") != 0)
{
break;
}
}
else
{
break;
}
}
}
void DeleteStudent()
{
char searchID[20];
bool found = false;
char input[1];
while(true)
{
printf("\nEnter the ID number of the student you want to delete: ");
scanf("%s", searchID);
for(int i = 0; i < sizeOfArray; i++)
{
if(strcmp(studentList[i].id, searchID) == 0)
{
found = true;
printf("Found!\n");
ShowStudentInfo(studentList[i]);
printf("\nYou really want to delete this data? [Y/N]:");
scanf("%s", input);
if(strcmp(input, "Y") == 0)
{
for (int j = i; j < sizeOfArray - 1; j++)
{
studentList[j] = studentList[j+1];
}
printf("\n\nSuccessfully deleted the student");
sizeOfArray--;
cin.get();
cin.get();
break;
}
}
}
if(!found)
{
printf("Student not found, try again? [Y/N]: ");
scanf("%s", input);
if(strcmp(input, "Y") != 0)
{
break;
}
}
else
{
break;
}
}
}
void SearchStudent()
{
char searchID[20];
bool found = false;
char input[1];
while(true)
{
printf("\nEnter the ID number of the student: ");
scanf("%s", searchID);
for(int i = 0; i < sizeOfArray; i++)
{
if(strcmp(studentList[i].id, searchID) == 0)
{
found = true;
ShowStudentInfo(studentList[i]);
cin.get();
cin.get();
}
}
if(!found)
{
printf("Student not found, try again? [Y/N]: ");
scanf("%s", input);
if(strcmp(input, "Y") != 0)
{
break;
}
}
else
{
break;
}
}
}
void ShowStudentHighestToLowest()
{
for(int i = 0; i < sizeOfArray - 1; i++)
{
for(int j = 0; j < sizeOfArray - 1; j++)
{
if(studentList[i].fgrade < studentList[j].fgrade)
{
Student temp = studentList[i];
studentList[i] = studentList[j];
studentList[j] = temp;
}
}
}
for(int i = 0; i < sizeOfArray - 1; i++)
{
ShowStudentInfo(studentList[i]);
}
cin.get();
cin.get();
}
void ShowStudentLowesToHighest()
{
for(int i = 0; i < sizeOfArray - 1; i++)
{
for(int j = 0; j < sizeOfArray - 1; j++)
{
if(studentList[i].fgrade > studentList[j].fgrade)
{
Student temp = studentList[i];
studentList[i] = studentList[j];
studentList[j] = temp;
}
}
}
for(int i = 0; i < sizeOfArray - 1; i++)
{
ShowStudentInfo(studentList[i]);
}
cin.get();
cin.get();
}
void SaveToFile()
{
fclose(fopen("Record.txt", "w"));
f = fopen("Record.txt", "w");
if(f == NULL)
{
printf("Failed to open the file...");
}
else
{
for(int i = 0; i < sizeOfArray; i++)
{
if(strcmp(studentList[i].name, "") != 0)
{
fprintf(f, "%s", studentList[i].id);
fprintf(f, "\n%s", studentList[i].name);
fprintf(f, "\n%s", studentList[i].course);
fprintf(f, "\n%d\n", studentList[i].year);
for(int j = 0; j < 3; j++)
{
fprintf(f,"%.2lf\n",studentList[i].score[j]);
fprintf(f,"%.2lf\n",studentList[i].total[j]);
fprintf(f,"%.2lf\n",studentList[i].grade[j]);
}
fprintf(f,"%.2lf\n",studentList[i].fgrade);
}
}
}
fclose(f);
}
void SetStudentData()
{
int index = 0;
int c;
f = fopen("Record.txt", "r");
if(f == NULL)
{
printf("The file is not existing...");
}
else
{
while ((c = fgetc(f)) != EOF)
{
ungetc(c, f);
fscanf(f,"%s",studentList[index].id);
fscanf(f,"%s",studentList[index].name);
fscanf(f,"%s",studentList[index].course);
fscanf(f,"%d",&studentList[index].year);
for(int i = 0; i < 3; i++)
{
fscanf(f,"%lf",&studentList[index].score[i]);
fscanf(f,"%lf",&studentList[index].total[i]);
fscanf(f,"%lf",&studentList[index].grade[i]);
}
fscanf(f,"%lf",&studentList[index].fgrade);
index++;
sizeOfArray++;
}
}
fclose(f);
}
void ShowList()
{
for(int i = 0; i < sizeOfArray - 1; i++)
{
printf("ID Number: %s\n", studentList[i].id);
printf("Name: %s\n", studentList[i].name);
printf("Course: %s\n", studentList[i].course);
printf("Year Level: %d\n", studentList[i].year);
for(int a = 0; a < 3; a++)
{
printf("Score [%d]: %.2f\n", a, studentList[i].score[a]);
printf("Total Score [%d]: %.2f\n", a, studentList[i].total[a]);
printf("Grade [%d]: %.2f\n", a, studentList[i].grade[a]);
}
printf("Final Grade: %.2f\n\n", studentList[i].fgrade);
}
cin.get();
cin.get();
}
void ShowStudentInfo(Student student)
{
printf("ID Number: %s\n", student.id);
printf("Name: %s\n", student.name);
printf("Course: %s\n", student.course);
printf("Year Level: %d\n", student.year);
for(int a = 0; a < 3; a++)
{
printf("Score [%d]: %.2f\n", a, student.score[a]);
printf("Total Score [%d]: %.2f\n", a, student.total[a]);
printf("Grade [%d]: %.2f\n", a, student.grade[a]);
}
printf("Final Grade: %.2f\n\n", student.fgrade);
}