Airline Booking System - PLZ HELP

This is a discussion on Airline Booking System - PLZ HELP within the C Programming forums, part of the General Programming Boards category; I'm writing a program for an airline booking system that reads in an external file called tickets.txt that looks like ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    Airline Booking System - PLZ HELP

    I'm writing a program for an airline booking system that reads in an external file called tickets.txt that looks like this:
    PHP Code:
    1 L.A4 Kennedy Tim
    1 L
    .A4 Wu Wanmin
    1 L
    .A7 Jain Ankur
    1 L
    .A4 Sen KK
    3 L
    .A4 Wu Wanmin
    2 L
    .A4 Kennedy Tim
    1 L
    .A7 Zaveri Sonia
    1 L
    .A4 Harrison P
    3 L
    .A4 Kennedy Tim
    4 L
    .A4
    5 L
    .A4
    1 L
    .A4 Patterson Justin
    1 L
    .A4 Rogers Anne
    1 L
    .A4 Moore Kim
    1 L
    .A4 Taylor Tim
    1 L
    .A4 Smith Adam
    1 L
    .A4 Ghosh Pinaki
    2 L
    .A4 Taylor Tim
    4 L
    .A4
    4 L
    .A7
    5 L
    .A4
    5 L
    .A7

    1 -- Book new flight
    2 -- Cancel existing flight
    3 -- Confirm flight
    4 -- Print passenger list
    5 -- Print seats available (0 = seat is taken)
    6 -- end program

    For example,
    1 L.A. 4 Smith Adam
    Booking a reservation for Smith Adam from L.A. on May 4

    My program seems to perform every task, but when it reaches the second print call (the "5 L.A. 4" "5 L.A. 7" right before the end of the input file) it doesn't print the whole list. I'm new to C, but I think it may have something to do with my using a global linked list? Maybe the function isn't starting at the begining of the list?? Any help and/or suggestions would be much appreciated.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    typedef struct node{
    	char flight[6];
    	int date;
    	char name[40];
    	int seat;
    	struct node *next;
    	//	struct node *prev;
    };
    int May[30][8];
    struct node *plane;
    
    int Book(int day, char name[40]);  //add new flight reservation
    int Cancel(char name[40], int day);  //cancel a current flight reservation
    int Confirm(char name[40], int day);  //confirm that a flight reservation exists
    void PrintList(int day);  //print the current list of passengers
    
    int main(void){
    	int x =0, y=0, action=0, day=0;
    	char userselection[30], name[40], date;
    	char filename[30];
    	FILE *fp;
    	printf("Please enter the filename containing passenger information:  \n");
    	scanf("%s", filename);
    	fp = fopen(filename, "r");
    	//Check to see if file exists
    	if(fp == NULL){ 
    		printf("\n\nError Opening File\n");
    		return 0;
    	}
    	for(x=1;x<32;x++){
    		for(y=0;y<8;y++){
    			May[x][y] = y+1;
    		}
    	}
    	
    	fgets(userselection,30,fp);
    	action = userselection[0];
    	while(action != '6'){
    		date = userselection[7];
    		day = (int)(date - '0');
    		y=9;
    		for(x=0; x<20;x++){
    			name[x]=userselection[y];
    			y++;
    		}
    		//if the file being read is a 1, add that person to the flight list
    		if(action == '1'){
    			Book(day, name);
    		}
    		//if the file being read is a 2, cancel that person's flight reservation
    		if(action == '2'){
    			Cancel(name, day);
    		}
    		//if the file being read is a 3, confirm that that person is on the list
    		if(action == '3'){
    			Confirm(name, day);
    		}
    		//if the file being read is a 4, print the current list of passengers
    		if(action == '4'){
    			PrintList(day);
    		}
    		//if the file being read is a 5, print the seating plan for the flight
    		if(action == '5'){
    			printf("The seating plan for flight ZM101 on May %d is:\n", day);
    			printf("[ %d %d %d %d %d %d %d %d ]\n", May[day][0],May[day][1],May[day][2],May[day][3],May[day][4],May[day][5],May[day][6],May[day][7]);
    		}
    		//if the file being read is a 6, quit the program
    		if(action == '6'){
    			printf("Quit the program");
    			printf("\n");
    			return 0;
    		}
    		//move to the next line in the file and read it
    		fgets(userselection,30,fp);
    		action = userselection[0];
    		printf("\n");
    	}
    	return 0;
    }
    
    //add a new reservation to the flight
    int Book(int day, char name[30]){
    	int sum=0,x;
    	char ZM101[6] = {'Z','M','1','0','1'};
    	struct node * pNew=(struct node *) (malloc(sizeof(struct node)));
    	for(x=0;x<8;x++){
    		sum+=May[day][x];
    	}
    	if(sum == 0){
    		printf("Sorry all seats are booked for ZM101 May %d\n", day);
    		return 0;
    	}
    	pNew->date =  day ;
    	strcpy(pNew->flight, ZM101);
    	strcpy(pNew->name, name);
    
    	//assigns seat number 1 to the passenger if that seat is empty
    	if(May[day][0] != 0){
    		pNew->seat= May[day][0];
    		May[day][0] = 0;
    	}
    	//else assigns seat number 2 to the passenger if that seat is empty
    	else if(May[day][1] != 0){
    		pNew->seat= May[day][1];
    		May[day][1] = 0;
    	}
    	//else assigns seat number 3 to the passenger if that seat is empty
    	else if(May[day][2] != 0){
    		pNew->seat= May[day][2];
    		May[day][2] = 0;
    	}
    	//else assigns seat number 4 to the passenger if that seat is empty
    	else if(May[day][3] != 0){
    		pNew->seat= May[day][3];
    		May[day][3] =0;
    	}
    	//else assigns seat number 5 to the passenger if that seat is empty
    	else if(May[day][4] != 0){
    		pNew->seat= May[day][4];
    		May[day][4] =0;
    	}
    	//else assigns seat number 6 to the passenger if that seat is empty
    	else if(May[day][5] != 0){
    		pNew->seat= May[day][5];
    		May[day][5] =0;
    	}
    	//else assigns seat number 7 to the passenger if that seat is empty
    	else if(May[day][6] != 0){
    		pNew->seat= May[day][6];
    		May[day][6]=0;
    	}
    	//else assigns seat number 8 to the passenger if that seat is empty
    	else if(May[day][7] != 0){
    		pNew->seat= May[day][7];
    		May[day][7] =0;
    	}
    
    	pNew->next = NULL;
    	//if the list is empty
    	if(plane== NULL){
    		plane  =  pNew;
    	}
    	else
    	//assign it the next open value in the list
    	{
    		pNew->next  = plane->next;
    		plane->next = pNew;
    	}
    	printf("%s  Booked on ZM101 on May %d\n",name, day);
    	return 1;
    }
    
    
    //Free's the seat number and reassigns, but doesn't remove the passenger...
    int Cancel(char name[40], int day){
    	struct node* current = plane;
    	if(current->date == day){
    		//if the date is found see if the name is the same
    		if(strcmp(name, current->name) == 0){
    			//put the seat number back into the array
    			May[day][current->seat - 1] = current->seat;
    			current = plane;
    			plane = current->next;
    			current = NULL;
    			printf("Booking cancelled for %s for flight ZM101 May %d\n", name, day);
    			return 0;
    		}
    	}
    	while(current->next != NULL){
    		//Check if its the correct date
    		if(current->next->date == day){
    			//if the date is found see if the name is the same
    			if(strcmp(name, current->next->name) == 0){
    				//put the seat number back into the array
    				May[day][current->next->seat - 1] = current->next->seat;
    				current->next = plane->next;
    				plane->next = current->next->next;
    				current->next = NULL;
    				printf("Booking cancelled for %s for flight ZM101 May %d\n", name, day);
    				return 0;
    			}
    		}
    		current = current->next;
    	}
    	printf("No flight reservation to cancel for %s on ZM101 on May %d\n", name, day);
    	return 1;
    }
    
    //confirm that the person has a reservation
    int Confirm(char name[40], int day){
    	struct node* current = plane;
    	while(current != NULL){
    		if(current->date == day){
    			if(strcmp(name, current->name) == 0){
    				printf("Yes, Booking confirmed for %s for flight ZM101 on May %d\n", name, day);
    				return 0;
    			}
    		}
    		current = current->next;
    	}
    	printf("Sorry, no booking found for %s for flight ZM101 on May %d\n", name, day);
    	return 0;
    }
    
    //Print the current list of passengers
    void PrintList(int day){
    	struct node* current = plane;
    	printf("\nPassenger List for ZM101 on May %d:\n",day);
    	while (current != NULL){
    		if(current->date == day){
    			printf("%s", current->name);
    			printf("%d", current->seat);
    			printf("\n");
    		}
    		current = current->next;
    	}
    	printf("\n");
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    One thing I see is...
    Code:
    char ZM101[6] = {'Z','M','1','0','1'};
    ...
    strcpy(pNew->flight, ZM101);
    ZM101 needs to be nul-terminated.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Also, all of this:
    Code:
    	if(May[day][0] != 0){
    		pNew->seat= May[day][0];
    		May[day][0] = 0;
    	}
    	//else assigns seat number 2 to the passenger if that seat is empty
    	else if(May[day][1] != 0){
    		pNew->seat= May[day][1];
    		May[day][1] = 0;
    	}
    seems unnecessary. You could do it in a for loop like:
    Code:
    for(i = 0;i < 8;++i)
    {
      if(May[day][i])
      {
        pNew->seat = &May[day][i];
        May[day][i] = 0;
      }
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    1. A major problem... you never set your plane pointer to NULL. Add plane = NULL; close to the top of main somewhere.

    2. In your Book function:
    Code:
    pNew->next = NULL;
    //if the list is empty
    if(plane== NULL){
        plane  =  pNew;
    }
    else
    //assign it the next open value in the list
    {
        pNew->next  = plane->next;
        plane->next = pNew;
    }
    I believe this is wrong. I think it should be:

    Code:
    pNew->next = NULL;
    //if the list is empty
    if(plane== NULL){
        plane  =  pNew;
    }
    else
    //assign it the next open value in the list
    {
        pNew->next  = plane;
        plane = pNew;
    }
    3.
    Code:
    //Free's the seat number and reassigns, but doesn't remove the passenger...
    int Cancel(char name[40], int day){
        struct node* current = plane;
        if(current->date == day){
            //if the date is found see if the name is the same
            if(strcmp(name, current->name) == 0){
                //put the seat number back into the array
                May[day][current->seat - 1] = current->seat;
                current = plane;  // No effect, current already equal to plane at this stage
                plane = current->next;
                current = NULL;  // Not needed
                printf("Booking cancelled for %s for flight ZM101 May %d\n", name, day);
                return 0;
            }
        }
        while(current->next != NULL){
            //Check if its the correct date
            if(current->next->date == day){
                //if the date is found see if the name is the same
                if(strcmp(name, current->next->name) == 0){
                    //put the seat number back into the array
                    May[day][current->next->seat - 1] = current->next->seat;
                    current->next = plane->next;
                    plane->next = current->next->next;
                    current->next = NULL;  // Not needed
                    printf("Booking cancelled for %s for flight ZM101 May %d\n", name, day);
                    return 0;
                }
            }
            current = current->next;
        }
        printf("No flight reservation to cancel for %s on ZM101 on May %d\n", name, day);
        return 1;
    }
    The above code creates memory leaks. Your reassignment of pointers causes you to lose track of some nodes. Check your logic carefully here. Plot it out using pen and paper if you don't see this. I suggest changing to this:
    Code:
    //Free's the seat number and reassigns, but doesn't remove the passenger...
    int Cancel(char name[40], int day){
        struct node* current = plane;
        if(current->date == day){
            //if the date is found see if the name is the same
            if(strcmp(name, current->name) == 0){
                //put the seat number back into the array
                May[day][current->seat - 1] = current->seat;
                plane = current->next;
                free(current);
                printf("Booking cancelled for %s for flight ZM101 May %d\n", name, day);
                return 0;
            }
        }
        while(current->next != NULL){
            //Check if its the correct date
            if(current->next->date == day){
                //if the date is found see if the name is the same
                if(strcmp(name, current->next->name) == 0){
                    //put the seat number back into the array
                    May[day][current->next->seat - 1] = current->next->seat;
                    struct node *temp = current->next;
                    current->next = current->next->next;
                    free(temp);
                    printf("Booking cancelled for %s for flight ZM101 May %d\n", name, day);
                    return 0;
                }
            }
            current = current->next;
        }
        printf("No flight reservation to cancel for %s on ZM101 on May %d\n", name, day);
        return 1;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Wow, another example of write all the code, then dump it on a message board for debugging

    > ZM101 needs to be nul-terminated.
    It will be itsme86, unspecified elements are set to 0. Since the array in question has 6 elements and 5 initialisers, all is well.

    This is better
    Code:
    char ZM101[] = "ZM101";
    > for(x=1;x<32;x++)
    Array smashing for fun and profit!
    Arrays start at 0, and since you have a [30], you should have <30, not <32
    Who knows where the rest is going

    > name[x]=userselection[y];
    A somewhat long-winded way of copying a string, but you forgot to append a \0 to make it a name into a proper c-style string.

    Plenty more later no doubt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Its still a little sloppy, but its working! Thanks for the help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 12:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 06:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 04:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21