I have made a program for hotel management. It has a problem that: In the Accounts function, it should reset all the flags of the registered members through the program. But there is a error that it does NOT. I have spent a lot of months, trying to debug this problem, but i couldn't. So please help me. Here is the code of the account function,
void accounts()
{
int ttt=0;
struct person payment;
char aname[21], oname[21];
char *namea;
int chec=1, ver=0;
long int recsize;
recsize=sizeof(payment);
f=fopen("C:\\HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!");
sleep(4);
exit(0);
}
clrscr();
cprintf("\n *** Pearl Guest House - Payments ***\n\n");
cprintf("\r\r\rEnter the Name :");
fflush(stdin);
scanf("%[^\n]s", &aname);
namea=strupr(aname);
strcpy(oname, namea);
while(fread(&payment, recsize, 1, f) == 1){
if((payment.flag == 1) && (strcmp(payment.name,oname) == 0)){
payment.pay=1;
printf("\n\n Payment Received");
fflush(stdin);
getch();
ver=1;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, sizeof(payment), 1, f);
break;
}
}
if(ver!=1){
printf("\n\n Record not Found!!!");
fflush(stdin);
getch();
}
//rewind(f);
fclose(f);
f=fopen("C:\\HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!");
sleep(4);
exit(0);
}
while(fread(&payment, recsize, 1, f) == 1){
if(payment.pay==0){
chec=0;
break;
}
}
//rewind(f);
f=fopen("C:\\HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!");
sleep(4);
exit(0);
}
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0");
fflush(stdin);
getch();
}
printf("Before Fclose");
fclose(f);
printf("After Fclose");
}
This is my structure person:
struct s_office{
char name[16];
char phone[12];
};
struct permanent{
char addr[100];
char phone[12];
};
struct emergency{
char name[21];
char relation[11];
char phone[12];
};
struct person{
char name[21];
char phone[12];
char place[21];
int roomno;
int flag;
char food;
struct s_office office;
char father[21];
char fphone[12];
struct permanent per;
struct emergency emer1;
char email[40];
int finger;
char dob[8];
int cidate;
int cimonth;
int ciyear;
int codate;
int comonth;
int coyear;
int rent;
int pay;
};
Here is a list of headers i included:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<dos.h>
I used Turbo C++ to write this code. When this is run, it runs successfully but the output is not what is supposed to be. (It is supposed to open the file(where is store data - HOTEL.DAT), read all the written functions one by one and then if it sees that all of them have pay variable = 1; it should say "All payments received..." and then it should set all of them to 0) I tried to debug and found out that most probably, the error is here:
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
}
Here I suppose that the loop is ok because if i comment the contents of the loop, it runs n times. (if n = number of entries) BUT if i uncomment the contents it does not! This is the problem. Therefore it doesn't set all of them to 0. What i want is a solution to this problem and code so that it gives desired output(of setting every pay variable to 0)
Remember: This is a 14 year old kid trying to code; please help and answer politely and in detail;;; THANKS