The program is working except for few problems:
1. If I initialize the cust_name with space it will not display apt_id for other items when I run the program next time but if I initialize it with any character it will be ok.
2. When I add a cust_name with more than 4 character it will add to it some strange characters.
3. How can I pause to let the user see some data when he select an option.
4. When I enter an option not in the menu the menu is then displayed more then once.
Any other hints that let me improve my program is appreciated.
Thank you.
Code:
/*A simple program that maintain apartment reservation
and it use struct and array and it save data in a text file */
#include <stdio.h>
/* num_lines is the number of screen display lines */
#define num_lines 25
struct appts
{
int apt_id;
char vacant;
char floor;
char *cust_name;
int no_of_days;
};
struct appts apt[20];
/* Function prototypes */
void CLS(void);
void DISP_DATA(void);
void GET_DATA(void);
void SAVE_DATA(void);
void LIST_VACNT(void);
void RESERV_APT(void);
void DEL_RESRV(void);
void MENU(void);
/*****************************************************************************/
void CLS()
{
int n;
for(n = 0; n < num_lines; n++)
puts("");
}
/*****************************************************************************/
void GET_DATA()
{
FILE *fp;
int i;
CLS();
fp=fopen("txtfile.txt","r");
if (fp == NULL)
for (i=0;i<20;i++)
{
apt[i].apt_id = i + 100;
apt[i].vacant = 'n';
apt[i].floor= '1';
apt[i].cust_name=' ';
apt[i].no_of_days=0;
}
else
{
for (i=0;i<20;i++)
{
fscanf(fp,"%d\n",&apt[i].apt_id);
fscanf(fp,"%c\n",&apt[i].vacant);
fscanf(fp,"%c\n",&apt[i].floor);
fscanf(fp,"%s\n",&apt[i].cust_name);
fscanf(fp,"%d\n",&apt[i].no_of_days);
}
fclose(fp);
}
}
/*****************************************************************************/
void DISP_DATA()
{
/* Displays data */
int i;
char text[5];
CLS();
for (i=0;i<20;i++)
{
printf("\n-------------------------------------------------------\n");
printf("%d",apt[i].apt_id);
printf("%c",apt[i].vacant);
printf("%c",apt[i].floor);
printf("%s",&apt[i].cust_name);
printf("%d",apt[i].no_of_days);
}
}
/*****************************************************************************/
void LIST_VACNT()
{
int i;
CLS();
for(i=0;i<20;i++)
{
if (apt[i].vacant == 'n')
printf("\nvacant ===> %d",apt[i].apt_id);
}
}
/*****************************************************************************/
void RESERV_APT()
{
int x,y;
CLS();
printf("\nEnter aparment no");
scanf("%d",&y);
x=y-100;
if (apt[x].vacant=='n')
{
apt[x].vacant='y';
printf("\nEnter name");
scanf("%s",&apt[x].cust_name);
apt[x].no_of_days = 5;
}
else
printf("this apartment is reserved!!!!!!!");
}
/*****************************************************************************/
void DEL_RESRV()
{
int i,x,y;
char name[20];
CLS();
printf("\nEnter apt no");
scanf("%d",&y);
x=y-100;
if (apt[x].vacant=='y')
{
apt[x].vacant='n';
apt[x].cust_name= ' ';
apt[x].no_of_days= 0;
}
else
printf("this apt is not reserved!!!!!!!");
}
/*****************************************************************************/
void MENU()
{
char option;
char text[10];
do
{
CLS();
if (option == 'a')
DISP_DATA();
else if (option == 'b')
LIST_VACNT();
else if (option == 'c')
RESERV_APT();
else if (option == 'd')
DEL_RESRV();
else
{
printf("\n\t\t\tSelect Option");
printf("\n\n\t\t\t a) List all apartments");
printf("\n\n\t\t\t b) List vacant apartments");
printf("\n\n\t\t\t c) Reserve apartment");
printf("\n\n\t\t\t d) Delete apartment reservation");
printf("\n\n\t\t\t x) Exit");
printf("\n");
}
scanf("%c",&option);
}
while(option != 'x');
}
/*****************************************************************************/
void SAVE_DATA()
{
FILE *fp;
int i;
fp= fopen("txtfile.txt","w");
for (i=0;i<20;i++)
{
fprintf(fp,"%d\n",apt[i].apt_id);
fprintf(fp,"%c\n",apt[i].vacant);
fprintf(fp,"%c\n",apt[i].floor);
fprintf(fp,"%s\n",&apt[i].cust_name);
fprintf(fp,"%d\n",apt[i].no_of_days);
}
fclose(fp);
}
/*****************************************************************************/
void main()
{
CLS();
GET_DATA();
MENU();
SAVE_DATA();
}