Thread: Can't figure out problem with code

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    93

    Can't figure out problem with code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_NAME 30
    
    void InputFile (char* input_file);
    void BookingSystem (char* input_file);
    
    struct passenger
    {
       char name[2*MAX_NAME + 1];
       int flydate;           //Flight date
       int seat;
       struct passenger *next;
    };
    
    int seats[31][8];
    
    typedef struct passenger passenger;
    
    passenger* AddNode (char *name, int flydate, passenger *head);
    passenger* DeleteNode(char *name, int flydate, passenger *head);
    //passenger* passengerInfo(FILE *input_stream, int passengers_registered, int exam_number, passenger *head);
    //void passengerDisplay (passenger *head, int exam_number);
    void fill_seats();
    int seat_alloc(int flydate);
    void seat_taken(int i, int j);
    void seat_returned(int i, int j);
    void ConfirmBook(char *name, int flydate, passenger *head);
    void PList(int date, passenger *head);
    void Display (passenger *head);
    void SeatList(int date);
    
    
    int main ()
    
    {
    
        char input_file[BUFSIZ];
    
        InputFile(input_file);
    
        BookingSystem(input_file);
    
    	getchar();
        getchar();
    
    	return 0;
    }
    
    void InputFile (char* input_file)
    {
    
    	printf("Enter the name of the file containing the booking information.\n");
        scanf("%s", input_file);   //Retrieving filename from user
    
        return;
    }
    
    void BookingSystem (char* input_file) //Collect necessary information from file and use it for booking system
    {
         FILE *input_stream;
    
         char *file = input_file;
    
         //Variables to hold information from input file
         int command;
         int date;
         char dest[31];
         char fname[31];
         char lname[31];
         char *name;
    
         int counter = 0;
    
         passenger *head = NULL;           //Creation of Head for Sorted Linked List
         head = malloc(sizeof(passenger));
         head = NULL;
    
         //Filling seats
         fill_seats();
    
         if ( (input_stream = fopen(input_file, "r")) == NULL )
         {
            printf("Could not open file.\n\n");
            return;
         }
    
         puts("");
         while ( !(feof(input_stream)))
         {
    
               fscanf(input_stream, "%d", &command);
               counter++;
    
               if ( counter % 3 == 0 )
               {
                  counter = 0;
                  puts("");
               }
    
               switch ( command )
               {
                      case 1: fscanf(input_stream, "%s", dest);
                              fscanf(input_stream, "%d", &date);
                              fscanf(input_stream, "%s", lname);
                              fscanf(input_stream, "%s", fname);
    
                              strcpy(lname + strlen(lname), " ");
                              name = strcat(lname, fname);
    
                              head = AddNode(name, date, head);
                              break;
    
                      case 2: fscanf(input_stream, "%s", dest);
                              fscanf(input_stream, "%d", &date);
                              fscanf(input_stream, "%s", lname);
                              fscanf(input_stream, "%s", fname);
    
                              strcpy(lname + strlen(lname), " ");
                              name = strcat(lname, fname);
    
                              head = DeleteNode(name, date, head);
                              break;
    
                      case 3: fscanf(input_stream, "%s", dest);
                              fscanf(input_stream, "%d", &date);
                              fscanf(input_stream, "%s", lname);
                              fscanf(input_stream, "%s", fname);
    
                              strcpy(lname + strlen(lname), " ");
                              name = strcat(lname, fname);
    
                              ConfirmBook(name, date, head);
                              break;
    
                      case 4: fscanf(input_stream, "%s", dest);
                              fscanf(input_stream, "%d", &date);
    
                              PList(date, head);
                              break;
    
                              default: printf("Not handled yet.\n");
                              break;
    
                      case 5: fscanf(input_stream, "%s", dest);
                              fscanf(input_stream, "%d", &date);
    
                              SeatList(date);
                              break;
    
                      case 6: printf("Good Bye!");
                              fclose(input_stream);
                              return;
    
    
               }
    
         }
    
            fclose(input_stream);
    
            return;
    }
    
    passenger* AddNode (char *name, int flydate, passenger *head)
    {
         passenger *node, *temp;
         temp = head;
         node = malloc(sizeof(passenger));
    
         if ( !seat_alloc(flydate) )
         {
            printf("\nSorry, All seats booked  for  flight ZM101 for date %d.\n", flydate);
    
            return head;
         }
    
           node->seat = seat_alloc(flydate);
    
           seat_taken(flydate, node->seat);
    
           node->flydate = flydate;
    
         strcpy(node->name,name);
    
         if ( head == NULL )  //In case the list is empty
         {
             node->next = NULL;
             head = node;
             printf("%s booked on ZM101 May %d seat %d.\n", node->name, node->flydate, node->seat);
             return head;
         }
    
         if ( head->next == NULL ) //Case when there is only one node present
         {
            if ( (strcmp(name, head->name) == -1) )
            {
                node->next = head;
                head = node;
                printf("%s booked on ZM101 May %d seat %d.\n", node->name, node->flydate, node->seat);
                return head;
            }
    
            else
            {
                head->next = node;
                node->next = NULL;
                printf("%s booked on ZM101 May %d seat %d.\n", node->name, node->flydate, node->seat);
                return head;
            }
         }
    
         if ( (strcmp(name, head->name) == -1) )  //Case where the addition is lexically greater than the head
         {
            node->next = head;
            head = node;
            printf("%s booked on ZM101 May %d seat %d.\n", node->name, node->flydate, node->seat);
            return head;
         }
    
         else
         {
    
             while ( (strcmp(name, temp->next->name) != -1) && (temp->next != NULL) )
             {
                 if ( temp == NULL) break;
                 temp = temp->next;
             }
    
             if ( (temp->next == NULL) && (strcmp(name, temp->next->name) == -1) )
             {
                node->next = temp->next;
                temp = node;
                printf("%s booked on ZM101 May %d seat %d.\n", node->name, node->flydate, node->seat);
                return head;
             }
    
             if ( temp->next == NULL )
             {
                temp->next = node;
                node->next = NULL;
                printf("%s booked on ZM101 May %d seat %d.\n", node->name, node->flydate, node->seat);
                return head;
             }
    
             if ( strcmp(name, temp->next->name) == -1 )
             {
                 node->next = temp->next;
                 temp = node;
    
                 printf("%s booked on ZM101 May %d seat %d.\n", node->name, node->flydate, node->seat);
                 return head;
             }
          }
    }
    
    
    
    passenger* DeleteNode(char *name, int flydate, passenger *head)
    {
               passenger *temp;
               temp = head;
    
               if ( (strcmp(name, head->name) == 0) && head->flydate == flydate )   //Head Deletion
               {
                  head = head->next;
                  printf("Booking canceled for %s on ZM101 May %d seat %d.\n", temp->name, temp->flydate, temp->seat);
                  seat_returned(flydate, temp->seat);
                  free(temp);
                  return head;
               }
    
               if( head->next == NULL ) //One Node
               {
                   if ( (strcmp(name, head->name) == 0) && head->flydate == flydate )
                   {
                      head = NULL;
                      printf("Booking canceled for %s on ZM101 May %d seat %d.\n", temp->name, temp->flydate, temp->seat);
                      free(temp);
                      seat_returned(flydate, temp->seat);
                      return head;
                   }
    
                   printf("Reservation not found, could not be canceled.\n");
                   return head;
                }
    
    
    
               while ( temp->next != NULL && ((strcmp(name, temp->next->name) != 0) && (temp->next->flydate != flydate) ) )
               {
                     temp = temp->next;
               }
    
               if (temp->next == NULL )
               {
                  if ( (strcmp(name, temp->name) == 0) && (temp->flydate == flydate) )
                  {
                     temp = NULL;
                     printf("Booking canceled for %s on ZM101 May %d seat %d.\n", temp->name, temp->flydate, temp->seat);
                     seat_returned(flydate, temp->seat);
                     return head;
                   }
    
                   printf("Reservation not found, could not be canceled.\n");
                   return head;
                }
    
                if ( (strcmp(name, temp->next->name) == 0) && (temp->next->flydate == flydate) )
                {
                   temp = temp->next->next;
                   free(temp->next);
                   printf("Booking canceled for %s on ZM101 May %d seat %d.\n", temp->next->name, temp->next->flydate, temp->next->seat);
                   seat_returned(flydate, temp->seat);
                   return head;
                }
    
                printf("Reservation not found, could not be canceled.\n");
                return head;
    
    }
    
    int seat_alloc(int flydate)
    {
        int i;
    
        for ( i = 0; i < 8; i++ )
        {
            if ( seats[flydate-1][i] != 0 )
               return seats[flydate-1][i];
        }
    
        return 0; //Cycled through all seats, and all of them are zero
    }
    
    void fill_seats()
    {
         int i, j;
         int x = 1;
         int k = 0;
    
    
         for ( i = 0; i < 31; i++ )
             for ( j = 0; j < 8; j++ )
             {
                 seats[i][j] = x;
                 x++; //Continue to increment x in order to fill up seats properly
                 k++; //Counter to know when to reset x to one again
    
                 if ( k % 8 == 0 )
                  x = 1;
              }
    }
    
    void seat_taken(int i, int j)
    {
         seats[i-1][j-1] = 0;
    
         return;
    }
    
    void seat_returned(int i, int j)
    {
         seats[i-1][j-1] = j;
    
         return;
    }
    
    void ConfirmBook(char *name, int flydate, passenger *head)
    {
        while ( head != NULL )
        {
              if ( (strcmp(name, head->name) == 0) && (flydate == head->flydate) )
              {
                 printf("Yes, booking confirmed for %s for flight ZM101 for May %d.\n", name, flydate);
                 return;
              }
    
              head = head->next;
         }
    
         printf("No, booking is not confirmed for %s for flight ZM101 for May %d.\n", name, flydate);
         return;
    }
    
    void PList(int date, passenger *head)
    {
         printf("\nPassenger list for ZM101     May %d\n", date);
    
         while ( head != NULL )
         {
               if ( head->flydate == date )
               {
                  printf("\t%s %d\n", head->name, head->seat);
               }
    
               head = head->next;
          }
    
          printf("\n");
    }
    
    void Display (passenger *head)
    {
         while ( head != NULL )
         {
               printf("%s %d %d", head->name, head->flydate, head->seat);
               head = head->next;
          }
    }
    
    void SeatList(int date)
    {
         printf("Seat list for May %d [%d %d ", date, seats[date-1][0], seats[date-1][1]);
         printf("%d %d ", seats[date-1][2], seats[date-1][3]);
         printf("%d %d %d %d]\n", seats[date-1][4],seats[date-1][5], seats[date-1][6], seats[date-1][7]);
    }
    I can't figure out the problem with this code. I suspect it to be in the bold parts.

    Any ideas or hints, or anything in particular is greatly appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What do you expect the program to do and what is it (not) doing contrary to your expectations?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Ah, sorry. I was going to post that, but I forgot in my haste.

    The program reads in names and numbers from a text file. These names and numbers are used in order to deliniate what flight and what seat a passenger to a flight will want.

    My problem right now, is that the linked list should read in the names in alphabetical order...and it is messing up after two people. I believe this is the problem, and it is the bold part of the code.

    I am attaching the sample file as well, since I forgot to do so earlier.

    I have been nitpicking the code for a good while, and have basically narrowed it down to this section. But any fix I have tried only manages to further mess it up.

    Thanks for any help.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    A bit more explanation of what the problem is would help as itsme86 mentioned.
    EDIT: You posted while I was writing this up

    I did notice this though:
    Code:
         passenger *node, *temp;
         temp = head;
         node = malloc(sizeof(passenger));
    
         if ( !seat_alloc(flydate) )
         {
            printf("\nSorry, All seats booked  for  flight ZM101 for date %d.\n", flydate);
    
            return head;
         }
    . . .
    Specifically this line: node = malloc(sizeof(passenger));

    First, just to make sure you're getting the right type of pointer back, you want to typecast malloc's return as a pointer to a passenger:
    node = (passenger*)malloc(sizeof(passenger));

    Second, you should not call malloc yet. If you do, and the following statement:
    if ( !seat_alloc(flydate) )

    ends up being true, then you are calling:
    return head;

    to exit the function. What that does is it destroys the "node" variable, so you have now lost access to that memory you just set aside. Doing this once or twice is not a big deal. But should this happen a lot, you're left with a memory leak (which a very bad thing). Don't call malloc until you're absolutely positive you need it.

    And then, to follow up, once your program is about to exit, creating a function to traverse your linked list, and free each node, I.e.:
    free(MyNode);

    Will be a good idea to ensure that any memory you set aside is now available to be used again.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Also, check out this link for info in strcmp():
    http://msdn.microsoft.com/library/de...c_._mbscmp.asp

    I believe your comaprisons are a bit off.
    strcmp() returns
    an integer < 0 (not necesserily -1) if string1 < string2
    0 if string1 = string2
    and an integer > 0 if string1 > string2

    Perhaps that might fix a problem or two?
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't figure out why code is hanging up
    By Panserbjorn in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 05:09 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM