Jump to content

This causes a fault - what caused it?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
I might have gone another blind corner here but this is what I am trying to print out to
this below after obtaining the data from the user

Quote

Enter room ID

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


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);

}	



#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
A segmentation fault can be defined as an access violation, so somewhere in your code is accessing memory it should not be.

Do you have any idea what portion of code causes this error? Try finding codes that could look wrong, and take them out or try to understand and visualize how each array, structure or string is filled.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users