this below after obtaining the data from the user
Quote
Enter room ID
Enter start date
Enter end Date
Enter start date
Enter end Date
Print out:
Quote
Room No NumOfBeds Status startDate endDate
=================================================
R103 2 occupied 16/04/211 23/04/2011
R103 2 reserved 15/05/2011 18/05/2011
=================================================
R103 2 occupied 16/04/211 23/04/2011
R103 2 reserved 15/05/2011 18/05/2011
This code caused an segament fault
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
//int RoomReport()
{
typedef struct booking
{
char reservationID[5];
char customerID[5];
char ID[5];
int numOfBeds;
char startDate[12];
char endDate[12];
char status[20];
} booking_t;
booking_t bookingList[20];
FILE*f;
char line[100];
int count = 0;
int k;
char *item;
char str1[15];
char str2[15];
char str3[15];
f = fopen("booking.txt", "r");
while(fgets(line, 100, f))
{
//printf("%s", line);
item = strtok(line, ";");
strcpy(bookingList[count].reservationID, item);
item = strtok(NULL, ";");
strcpy(bookingList[count].customerID, item);
item = strtok(NULL, ";");
strcpy(bookingList[count].ID, item);
item = strtok(NULL, ";");
bookingList[count].numOfBeds = atoi(item);
item = strtok(NULL, ";");
strcpy(bookingList[count].startDate, item);
item = strtok(NULL, ";");
strcpy(bookingList[count].endDate, item);
item = strtok(NULL, ";");
strcpy(bookingList[count].status, item);
count++;
}
fclose(f);
while(str1 != 0)
{
// asks for the roomID
printf("\nEnter room ID(or 'exit' to terminate): ");
scanf("%s", str1);
if(strcmp(str1, "exit") ==0)
{
// if exit is selected, operation is terminated
printf("\n\nOperation terminated.\n");
printf("Goodbye!\n");
break;
}
if((strcmp(str1, "R121"))==0)
{
//if user inputs "R121", error input is printed
printf("\nERROR: Invalid roomID!\n");
continue;
}
printf("Enter start date: \n");
scanf("%s", str2);
//printf("Enter end date: \n");
//scanf("%s", str3);
if(strcmp(str1, bookingList[k].ID)==0)
if(strcmp(str2, bookingList[k].startDate)==0)
//if(strcmp(str3, bookingList[k].endDate)==0)
printf("\n-------------------------------------------------\n");
printf("Room No | numOfBeds | Status | startDate | endDate");
printf("\n=================================================\n");
for(k = 0; k < count; k++)
{
printf("%3s %9d %12s %14s %16s\n\n", bookingList[k].ID, bookingList[k].numOfBeds, bookingList[k].status, bookingList[k].startDate, bookingList[k].endDate);
}
}
return(0);
}


Sign In
Create Account


Back to top









