My class, my teacher and I all couldn't figure out what's wrong with the code. It's a canteen ordering system, so as a student you order something and it logs it into a system and the canteen workers can make food accordingly. The specific problem is that I'm trying to make a tally but it only prints the first food item that was ordered. The relevant code is below and images are attached.
Menu of available items, Orders Placed, Result of order tally //note the student's order is an ID of the food item
struct food
{
int foodID;
char foodName[namesize];//namesize is 30
char foodItems[size];//specials for the day//size is 3
int itemTally;//total of food that needs to be prepared - REPLACED
float totalEarnt;//total amount earnt for selling the food
float totalSpen;//cost of food to the employer
float totalProfits;//total profts made
float itemCost;//cost of the food item
};
struct student
{
char firstname[namesize];
char lastname[namesize];
int ID;//students ID
float form;//student's form room
int order;//ID of the food they ordered
int quantity;//how much of the food item
float paid;//how much they paid
int delivery;//This is a yes or no
char location[aSize];//where it will be delivered to; aSize is 20
};
void orderTally()//Tallies up all the orders based on their ID; Student 1 orders 3 fish, student 2 orders 4 fish, outputs 7 fish
{
FILE*fp;
FILE*fp1;
struct food foodie={0,"","",0,0.0,0.0,0.0,0.0};
struct student orderie={"","",0,0.0,0,0,0.0,0,""};
int i;//This is the food ID.
if((fp1 = fopen("student", "rb+"))==NULL)//order file
{
printf("Cannot open order file \n");
exit(1);
}
if ((fp = fopen("food", "rb+"))==NULL)//menu file
{
printf("Cannot open food file \n");//UPDATE: update other statements
exit(1);
}
int sum=0;
for(i=1;i<=100;i++)
{
fread(&orderie,sizeof(struct student),1,fp1);
while(!feof(fp1))
{
if(orderie.order==i)//If the order ID is equal to one of the 100 food IDs...
{
sum=sum+orderie.quantity;
}
fread(&orderie,sizeof(struct student),1,fp1);
}
if(sum!=0)
{
fseek(fp,(i-1)*sizeof(struct food),SEEK_SET);
fread(&foodie,sizeof(struct food),1,fp);
printf("------------------------------------------------------------\n");
printf("\t\t\tORDERS\n");
printf("------------------------------------------------------------\n\n");
printf("%s",foodie.foodName);
printf(" - %d\n\n",sum);
sum=0;
}
}
fclose(fp1);
fclose(fp);
}