whenever I load the data, the program only retrieve the first line of the file and then it loops. It looks like the problem lies within the load function and insert function because that's where the code loads the data from the file and where the data loaded insert to the array of the code, I tried adding a sign if the code really loops the, and it really did loop. I think the problem is !feof int the load function but i don't know if it really is.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <stdbool.h>
#define MAX 10
//Variables
int Ident[MAX];
float grade[MAX];
char name[MAX][20];
int last;
//Functions
int GetRec();
float ComputeGrade(int q1, int q2);
void display();
void EmpList();
int IsEmpty();
int IsFull();
int DelRec();
int menu();
void CheckID(int x);
void save();
void load();
void insert(int x, char n[], float a);
int main(){
EmpList();
load();
menu();
}
void EmpList(){
last = -1;
}
int IsFull(){
return (last==MAX - 1);
}
int IsEmpty(){
return (last==-1);
}
int menu(){
int Opt;
do{
system("cls");
printf("[1] Insert\n");
printf("[2] Delete\n");
printf("[3] Display\n");
printf("[4] Exit\n");
scanf("%d", &Opt);
switch(Opt){
case 1: system("cls");
GetRec();break;
case 2: DelRec();
break;
case 3: system("cls");
display();
break;
case 4: save();
return 0;
break;
default:menu();
break;
}
}while(1);
}
int GetRec(){
char NameTemp[20];
int quiz_1, quiz_2, Idtemp;
float ave;
printf("Insert Mode\n");
printf("Input ID no:");
scanf("%d", &Idtemp);
printf("Input name: ");
scanf("%s", &NameTemp);
printf("Input quiz 1: ");
scanf("%d", &quiz_1);
printf("Input quiz 2: ");
scanf("%d", &quiz_2);
ave = ComputeGrade(quiz_1, quiz_2);
CheckID(Idtemp);
insert(Idtemp, NameTemp, ave);
}
void CheckID(int x){
for(int check = 0; check <= last; check++){
if(x == Ident[check]){
printf("Id already exist\nTry another one.");
getch();
system("cls");
GetRec();
}
}
}
void insert(int x, char n[], float a){
char *ptr;
if (IsFull()){
printf("Full.");
getch();
menu();
}
else if(x == -1){
EmpList();
}
else{
last++;
printf("%d", last);
Ident[last] = x;
grade[last] = a;
ptr = n;
strcpy(name[last], n);
}
}
float ComputeGrade(int q1, int q2){
float ave;
ave = (q1 + q2) / 2.0;
return ave;
}
int DelRec(){
printf("Delete Mode\n");
printf("Input Id no: ");
getch();
main();
}
void display(){
if(IsEmpty()){
printf("Empty");
getch();
menu();
}
else{
for(int counter = 0; counter <= last; counter++){
printf("[%d]%s\t%.2f\n", Ident[counter], name[counter], grade[counter]);
}getch();
}
}
void save(){
FILE *fp;
fp=fopen("database.dbf","w+");
for(int counter = 0; counter <= last; counter++){
fprintf(fp, "%d %s %.2f\n", Ident[counter], name[counter], grade[counter]);
}
fclose(fp);
}
void load(){
FILE *fp;
int id = -1,grd = 0;
char nm[20];
fp=fopen("database.dbf","r+");
while(!feof(fp)){
fscanf(fp,"%d %s %d", &id, &nm, &grd);
insert(id, nm, grd);
}
fclose(fp);
}