GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2005, 15:18
batrsau batrsau is offline
New Member
 
Join Date: Mar 2005
Posts: 15
batrsau is on a distinguished road

Airport Log program using 3D linked List : problem reading from file


Hi !

This is a program i wrote which creates a 3D linked list. A short brefing of the program is as follows:

1. creates a linked list of all the airlines on the airport
2. Each airline node holds another node to generate a linked list of all the flights for that airline
3. Each flight node holds another node to generate a linked list of all the passengers travelling on the flight.

The program works fine without using files.

I am trying to implement this program using files and have ran into problems I cannot understand:

The read function fails to read properly and generate a proper 3D list. The only sucessfull list generated is the airline name list and it fails.

Failures include things like:

1. running into infinite loop when displaying a flight list for an airline
2. running into infinite loop when displaying passengers for a flight
3. if the first airline name has no flights and the second airline name has flights then the read function links the flights of second airline to the first airline also

Iam still testing more on this program and will keep trying to solve more bugs in it plus also try to solve the ones above

Here is quick breifing on how the files generated and read are structured : (All thanks to hk_mp5kpdw for this )

//write data to file before exiting the program
void write_to_file(airline *start1)

The structure of the written file is like this :

Quote:
[number of airlines]
[first airline's name]
[number of flights for first airline]
[first airline's, first flight's information]
[first airline's, first flight's number of passengers]
[first airline's, first flight's, first passengers information]
[first airline's, first flight's, next passengers information]
[etc...]
[first airline's, second flight's information]
[first airline's, second flight's number of passengers]
[first airline's, second flight's, first passenger's information]
[first airline's, second flight's, next passenger's information]
[etc...]
[etc...]
[second airline's name]
[etc...]

Now when i read the file this is the structure of the program i use:

start1 read_from_file();

CPP / C++ / C Code:
FILE* fp;
struct airline *temp;
int count = 0;

// Open file for writing
fp = fopen("airline.txt","wb");  // Note binary mode

// Determine number of airlines and write to file
for( temp = start1; temp != NULL; temp = temp->next ) ++count;
fwrite(&count,sizeof(int),1,fp);

// Loop through and write airlines data to file
for( temp = start1; temp != NULL; temp = temp->next )
{
    // Determine length of airline name and write to file
    int length = strlen(temp->name);
    fwrite(&length,sizeof(int),1,fp);

    // Write airline name to file
    fwrite(temp->name,length,1,fp);

    // Write airline flight information to file
    determine and write number of flights for current airline to file
    for( loop through flights for current airline )
    {
        // Write passenger data to file
        determine and write number of passengers for current flight to file
        for( loop through passengers in current flight )
        {
        }
    }
}
fclose(fp);

and finally here is the enitre code, I hope is not too big to post :


CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

 
struct flight;                               //Forward Declaration of Flight structure


//-----------------------------------   AIRLINE LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------



struct airline                          // Structure for Airline node, includes strings for Airline name 
{                                      // pointer locations to the next airline and also to the flight list for that airline         
	char *airlinename;
	struct airline *next;
	struct flight *node1;
} *start1 = NULL;


airline* create_airline_node(airline *start1);             //Function Declarations
airline* enter_airline_info(airline *node);
airline* delete_airline_node(airline *start1);
void print_airline_list(airline *start1);
airline* enter_flight_menu(airline *start1);
void write_to_file(airline *start1);
airline * read_from_file();
airline* airline_menu(airline *start1);




//-----------------------------------   FLIGHT LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------

struct passenger;                              //Forward Declaration of passenger structure


struct flight                          // Structure for flight node, includes strings for flightname, from, to and time with 
{                                      // pointer locations to the next flight and also to the passenger list          
	char *flightname;
	char *departure_location;
	char *arrival_location;
	char *take_off_time;
	struct flight *next;
	struct passenger *node2;
} *start2 = NULL;


flight* create_flight_node(flight *start2);             //Function Declarations
flight* enter_flight_info(flight *node);
flight* delete_flight_node(flight *start2);
void print_flight_list(flight *start2);
flight* enter_passenger_menu(flight *start2);
flight* flight_menu(flight *start2);



//-----------------------------------   PASSENGER LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------


struct passenger                                 //Passenger Structure includes name of the passenger and the ponter to the next
{                                                // passenger node, structure declaration also declares start as the first node
	char *name;                                  // and declares it NULL.
	struct passenger *next;
} *start3 = NULL;



passenger* create_passenger_node(passenger *start3);             //Function Declarations
passenger* enter_passenger_info(passenger *node);
passenger* delete_passenger_node(passenger *start3);
passenger* passenger_menu(passenger *start3);
void print_passenger_list(passenger *node);
 




//----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE AIRLINE LINKED LIST --------------------------------------------


airline* create_airline_node(airline *start1)         // Creates a sorted list of airlines (default entry sort condition by airline name)
{                                                   // by entering the airline name in a node at the sorted position, 
                                                    // it calls the function "enter_airline_info() to ask the 
	if (start1 == NULL)                             // user for its information
	{
		start1 = (airline *) malloc(sizeof(airline));
		start1 = enter_airline_info(start1);
		start1->next = NULL;
	}
	else
	{
		airline *count_node1, *count_node2, *node;
		node = (airline *) malloc(sizeof(airline));
		node = enter_airline_info(node);
		for (count_node1 = start1; (count_node1 != NULL) && (strcmp(count_node1->airlinename, node->airlinename) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
		if(count_node1 == start1)
		{
			node->next = start1;
			start1 = node;
		}
		else if (count_node1 == NULL)
		{
			count_node2->next = node;
			node->next = NULL;
		}
		else
		{
			count_node2->next = node;
			node->next = count_node1;
		}
	}
	return start1;
}


airline* enter_airline_info(airline *node)             // Function to enter the information about the airline, this function is
{                                                      // called from the function create_airline_node(airline *start1)by itself.   
	printf("\n");                                      
	printf(" Welcome to IGI International Airport\n");
	printf("Please enter the Airline Name\n");
	printf("\n");
	fflush(stdin);
	node->airlinename = new char [80];
	gets(node->airlinename);
	node->node1 = NULL;
	return node;
}

airline* delete_airline_node(airline *start)          // Function to delete the name of a airline, function 
{                                                    // searches for the name and deletes if it is found.
	if(start1 == NULL)
	{
		printf("\n");
		printf("Sorry, this Airport is not operational at this moment\n");
	}
	else
	{
		char airlinename[80];
		printf("\n");
		printf("Please enter the  name of the Airline you want to delete\n");
		fflush(stdin);
		gets(airlinename);
		airline *count_node1, *count_node2;
		airline *temp;
		for(count_node1 = start1; (count_node1 != NULL) && (strcmp(count_node1->airlinename, airlinename) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
		if(count_node1!= NULL)
		{
			if (count_node1 == start1)
			{
				temp = start1;
				start1 = start1->next;
				free(temp);
			}
			else if(count_node1->next == NULL && count_node1 != start1) 
			{
				temp = count_node1;
				count_node2->next = NULL;
				free (temp);
			}
			else
			{
				temp = count_node1;
				count_node2->next = count_node1->next;
				free (temp);
			}
		}
		else 
		{
			printf("\n");
			printf("Sorry, this airline is currently not in service at this airport\n");
		} 
	}
	return start1;
}


void print_airline_list(airline *start)                       //Function to print all the airlines operating on the airport
{
	if (start1 == NULL)
	{
		printf("\n");
		printf("Sorry, this Airport is not operational at this moment\n");
	}
	else
	{
		printf("\n");
		airline *count;
		for (count = start1; count != NULL; count = count->next)
		printf("%s\n", count->airlinename);
	}
}

airline* enter_flight_menu(airline *start1)        // Function to search for the airline entered and enter its flight list menu
{
	char airlinename[80];
	printf("\n");
	printf("Welcome to the Flight list Menu\n");
	printf("Enter the Airline name you wish to check\n");
	fflush(stdin);
	gets(airlinename);
	airline *count;
	for(count = start1; (count != NULL) && (strcmp(count->airlinename, airlinename) != 0); count = count->next);
	if(count == NULL)
	{
		printf("\n");
		printf("Sorry, this airline does not operate in this airport\n");
		return start1;
	}
	else
	{
		printf("\n");
		count->node1 = flight_menu(count->node1);
		return start1;
	}
}


void write_to_file(airline *start1)
{
	FILE *fp;
	airline *count1;
	flight *count2;
	passenger *count3;
	int length = 0;
	int airline_count = 0;
	int passenger_count = 0;
	int flight_count = 0;
	fp = fopen("airline.txt","wb");

	// Determine number of airlines and write to file
	for( count1 = start1; count1 != NULL; count1 = count1->next) ++airline_count;
	fwrite(&airline_count,sizeof(int),1,fp);


	// Loop through and write airlines data to file
	for( count1 = start1; count1 != NULL; count1 = count1->next)
	{
		// Determine length of airline name and write to file
		length = strlen(count1->airlinename);
		fwrite(&length,sizeof(int),1,fp);

		// Write airline name to file
		fwrite(count1->airlinename,length,1,fp);

		// Determine number of flights for the airline and write to file
		for( count2 = count1->node1; count2 != NULL; count2 = count2->next) ++flight_count;
		fwrite(&flight_count,sizeof(int),1,fp);

		// Loop through and write flight data for the airline to file
		for( count2 = count1->node1; count2 != NULL; count2 = count2->next)
		{
			// Determine length of flight name and write to file
			length = strlen(count2->flightname);
			fwrite(&length,sizeof(int),1,fp);

			// Write flight name to file
			fwrite(count2->flightname,length,1,fp);

			// Determine length of departure location and write to file
			length = strlen(count2->departure_location);
			fwrite(&length,sizeof(int),1,fp);

			// Write departure location to file
			fwrite(count2->departure_location,length,1,fp);

			// Determine length of arrival location and write to file
			length = strlen(count2->arrival_location);
			fwrite(&length,sizeof(int),1,fp);

			// Write departure location to file
			fwrite(count2->arrival_location,length,1,fp);

			// Determine length of take off time and write to file
			length = strlen(count2->take_off_time);
			fwrite(&length,sizeof(int),1,fp);

			// Write departure location to file
			fwrite(count2->take_off_time,length,1,fp);


			// Determine number of passengers for this flight and write to file
			for( count3 = count2->node2; count3 != NULL; count3 = count3->next) ++passenger_count;
			fwrite(&passenger_count,sizeof(int),1,fp);

			// Loop through and write passenger data for the flight to file
			for( count3 = count2->node2; count3 != NULL; count3 = count3->next)
			{
				// Determine length of passenger name and write to file
				length = strlen(count3->name);
				fwrite(&length,sizeof(int),1,fp);

				// Write passenger name to file
				fwrite(count3->name,length,1,fp);
			}


		}


	}
	fclose(fp);

}


airline * read_from_file()
{
	FILE *fp;
	airline *start, *curr, *temp;
	int length = 0;
	int airline_count= 0;
	int flight_count = 0;
	int passenger_count = 0;

	// Initialize pointers
	start = curr = NULL;

	// Open file for reading
	fp = fopen("airline.txt","rb");

	while(!feof(fp))
	{
		// Read in number of airlines stored in file
		if( fp == NULL)
			airline_count = 0;
		else
			fread(&airline_count,sizeof(int),1,fp);

		// Loop through "airline_count" airlines
		for( ; airline_count > 0; --airline_count )
		{
			// allocate room for a single airline structure and flight link to NULL
			temp = (airline *)malloc(sizeof(airline));
			temp->next = NULL;
			temp->node1 = NULL;

			// read airline name length
			fread(&length,sizeof(int),1,fp);

			//allocate sufficent memory for the airline name
			temp->airlinename = (char *)malloc(length+1);

			// read in airline's name and NULL terminate
			fread(temp->airlinename,length,1,fp);
			temp->airlinename[length] = 0;

			// Add current airline to end of growing list of airlines
			if( start == NULL ) 
			{
				start = curr = temp;
			}
			else
			{
				curr->next = temp;
				curr = curr->next;
			}

			// Read in number of flights stored for the airline in file
			fread(&flight_count,sizeof(int),1,fp);

			// Loop through "flight_count" flight
			for( ; flight_count > 0; --flight_count )
			{
				// allocate room for a single flight structure and set passenger link to NULL
				curr->node1 = (flight *)malloc(sizeof(flight));
				curr->node1->next = NULL;
				curr->node1->node2 = NULL;

				// read flight name length
				fread(&length,sizeof(int),1,fp);

				//allocate sufficent memory for the flight name
				curr->node1->flightname = (char *)malloc(length+1);

				// read in flight name and NULL terminate
				fread(curr->node1->flightname,length,1,fp);
				curr->node1->flightname[length] = 0;

				// read departure location length
				fread(&length,sizeof(int),1,fp);

				//allocate sufficent memory for the departure location
				curr->node1->departure_location = (char *)malloc(length+1);

				// read in departure location and NULL terminate
				fread(curr->node1->departure_location,length,1,fp);
				curr->node1->departure_location[length] = 0;

				// read arrival location length
				fread(&length,sizeof(int),1,fp);

				//allocate sufficent memory for the arrival location
				curr->node1->arrival_location = (char *)malloc(length+1);

				// read in arrival location and NULL terminate
				fread(curr->node1->arrival_location,length,1,fp);
				curr->node1->arrival_location[length] = 0;

				// read take off time length
				fread(&length,sizeof(int),1,fp);

				//allocate sufficent memory for the take off time
				curr->node1->take_off_time = (char *)malloc(length+1);

				// read in take off time and NULL terminate
				fread(curr->node1->take_off_time,length,1,fp);
				curr->node1->take_off_time[length] = 0;

				// Add current flight to end of growing list of flights
				if( start->node1 == NULL ) 
				{
					start->node1 = curr->node1 = temp->node1;
				}
				else
				{
					curr->node1->next = temp->node1;
					curr->node1 = curr->node1->next;
				}

			
				// Read in number of passengers stored for that file
				fread(&passenger_count,sizeof(int),1,fp);

				// Loop through "passenger_count" passengers for this flight
				for( ; passenger_count > 0; --passenger_count )
				{
					// allocate room for a single passenger structure
					curr->node1->node2 = (passenger *)malloc(sizeof(passenger));
					curr->node1->node2->next = NULL;

					// read passenger name length
					fread(&length,sizeof(int),1,fp);

					//allocate sufficent memory for the passenger name
					curr->node1->node2->name = (char *)malloc(length+1);

					// read in passenger name and NULL terminate
					fread(curr->node1->node2->name,length,1,fp);
					curr->node1->node2->name[length] = 0;


					// Add current passenger to end of growing list of passengers for this flight
					if( start->node1->node2 == NULL ) 
					{
						start->node1->node2 = curr->node1->node2 = temp->node1->node2;
					}
					else
					{
						curr->node1->node2->next = temp->node1->node2;
						curr->node1->node2 = curr->node1->node2->next;
					}
				}

			}
		}
	}
	fclose(fp);
	return start;

}

airline* airline_menu(airline *start)                            // Menu Function which is handles the airlinelist
{                                                                      
	int choice;
	printf("Welcome to the Main Menu\n");
	printf("Please enter your choice from the menu shown below\n");
	printf("1: Add a new airline to this airport\n");
	printf("2: Delete an airline from the airport\n");
	printf("3: Display the Airlines List at this Airport\n");
    printf("4: Enter Flight menu for an airline\n");
	printf("5: Exit the program\n");
	scanf ("%d", &choice);
	switch(choice)
	{
	case 1: 
		{ 
			start1 = create_airline_node(start1);
		} break;
	case 2:
		{  
			start1 = delete_airline_node(start1);
		} break;
	case 3:
		{
			print_airline_list(start1);
		} break;
	case 4:
		{
			start1 = enter_flight_menu(start1);
		} break;
	case 5:
		{
			write_to_file(start1);
			exit(1);
		} break;
	default:
		{
			start1 = airline_menu(start1);
		} break;
	}
	printf("\n");
	start1 = airline_menu(start1);
	return start1;
}


//----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE FLIGHT LINKED LIST --------------------------------------------


flight* create_flight_node(flight *start2)           // Creates a sorted list of flights (default entry sort condition by flight no.)
{                                                    // by entering the flight node at the sorted position, 
    printf("\n");                                    // it calls the function "enter_flight_info() to ask the 
	if (start2 == NULL)                              // user for its information
	{
		start2 = (flight *) malloc(sizeof(flight));
		start2 = enter_flight_info(start2);
		start2->next = NULL;
	}
	else
	{
		flight *count_node1, *count_node2, *node;
		node = (flight *) malloc(sizeof(flight));
		node = enter_flight_info(node);
		for (count_node1 = start2; (count_node1 != NULL) && (strcmp(count_node1->flightname, node->flightname) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
		if(count_node1 == start2)
		{
			node->next = start2;
			start2 = node;
		}
		else if (count_node1 == NULL)
		{
			count_node2->next = node;
			node->next = NULL;
		}
		else
		{
			count_node2->next = node;
			node->next = count_node1;
		}
	}
	return start2;
}


flight* enter_flight_info(flight *node)               // Function to enter the information about the flight, this function is
{                                                    // called from the function create_flight_node(flight *start)by itself.   
	printf(" Welcome to the Flight List\n");
	printf("Please enter the Flight Name\n");
	fflush(stdin);
	node->flightname = new char [80];
	gets(node->flightname);
	printf("Please enter the Departure Location\n");
	fflush(stdin);
	node->departure_location = new char [80];
	gets(node->departure_location);
	printf("Please enter the Arrival Location\n");
	fflush(stdin);
	node->arrival_location = new char [80];
	gets(node->arrival_location);
	printf("Please enter the Take off time\n");
	fflush(stdin);
	node->take_off_time = new char [80];
	gets(node->take_off_time);
	node->node2 = NULL;
	return node;
}

flight* delete_flight_node(flight *start2)            // Function to delete the name of a flight, function 
{                                                     // searches for the name and deletes if it is found.
	printf("\n");
	if(start2 == NULL)
	{
		printf("Sorry, no available Flights today\n");
	}
	else
	{
		char flightname[80];
		printf("Please enter the flight name you want to delete\n");
		fflush(stdin);
		gets(flightname);
		flight *count_node1, *count_node2;
		flight *temp;
		for(count_node1 = start2; (count_node1 != NULL) && (strcmp(count_node1->flightname, flightname) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
		if(count_node1!= NULL)
		{
			if (count_node1 == start2)
			{
				temp = start2;
				start2 = start2->next;
				free(temp);
			}
			else if(count_node1->next == NULL && count_node1 != start2) 
			{
				temp = count_node1;
				count_node2->next = NULL;
				free (temp);
			}
			else
			{
				temp = count_node1;
				count_node2->next = count_node1->next;
				free (temp);
			}
		}
		else 
		{
			printf("\n");
			printf("Sorry, this name was not found in this list\n");
		} 
	}
	return start2;
}


void print_flight_list(flight *start2)
{
	printf("\n");
	if (start2 == NULL)
	{
		printf("Sorry, the Flight List is empty\n");
	}
	else
	{
		flight *count;
		for (count = start2; count != NULL; count = count->next)
		{
			printf("%s\n", count->flightname);
		    printf("From %s\n", count->departure_location);
		    printf("To %s\n", count->arrival_location);
		    printf("Take off Time : %s\n", count->take_off_time);
			printf("\n");
		}
	}
}

flight* enter_passenger_menu(flight *start2)        // Function to search for the flight entered and enter its passenger list menu
{
	printf("\n");
	char flightname[80];
	printf("Welcome to the passenger list Menu\n");
	printf("Enter the Flight name you wish to check\n");
	fflush(stdin);
	gets(flightname);
	flight *count;
	for(count = start2; (count != NULL) && (strcmp(count->flightname, flightname) != 0); count = count->next);
	if(count == NULL)
	{
		printf("Sorry, this flight is not available\n");
		return start2;
	}
	else
	{
		count->node2 = passenger_menu(count->node2);
		return start2;
	}
}

flight* flight_menu(flight *start2)                          // Menu Function which is handles the flightlist for an airline
{                                                                      
	int choice;
	printf("Welcome to the Flight menu\n");
	printf("Please enter your choice from the menu shown below\n");
	printf("1: Add a new Flight to this Airline\n");
	printf("2: Delete a Flight from the Airline\n");
	printf("3: Display the Flight List for the Airline\n");
    printf("4: Enter passenger menu for a flight\n");
	printf("5: Return back to the Main Menu\n");
	scanf ("%d", &choice);
	switch(choice)
	{
	case 1: 
		{ 
			start2 = create_flight_node(start2);
		} break;
	case 2:
		{  
			start2 = delete_flight_node(start2);
		} break;
	case 3:
		{
			print_flight_list(start2);
		} break;
	case 4:
		{
			start2 = enter_passenger_menu(start2);
		} break;
	case 5:
		{
			return start2;
		} break;
	default:
		{
			start2 = flight_menu(start2);
		} break;
	}
	printf("\n");
	start2 = flight_menu(start2);
	return start2;
}


//----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE  PASSENGER LINKED LIST --------------------------------------------




passenger* create_passenger_node(passenger *start3)  //Creates a sorted list of passengers by entering the passenger node at the
{                                                    // sorted position, it calls the function "enter_passenger_info() to
    printf("\n");                                    // ask the user for its information
	if (start3 == NULL)                              
	{
		start3 = (passenger *) malloc(sizeof(passenger));
		start3 = enter_passenger_info(start3);
		start3->next = NULL;
	}
	else
	{
		passenger *count_node1, *count_node2, *node;
		node = (passenger *) malloc(sizeof(passenger));
		node = enter_passenger_info(node);
		for (count_node1 = start3; (count_node1 != NULL) && (strcmp(count_node1->name, node->name) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
		if(count_node1 == start3)
		{
			node->next = start3;
			start3 = node;
		}
		else if (count_node1 == NULL)
		{
			count_node2->next = node;
			node->next = NULL;
		}
		else
		{
			count_node2->next = node;
			node->next = count_node1;
		}
	}
	return start3;
}


passenger* enter_passenger_info(passenger *node)     // Function to enter the name of the passenger, this function is called 
{                                                    // from the function create_passenger_node(passenger *start)by itself.   
	printf(" Welcome to the passenger list\n");
	printf("Please enter your name\n");
	fflush(stdin);
	node->name = new char [80];
	gets(node->name);
	return node;
}

passenger* delete_passenger_node(passenger *start3)   // Function to delete the name of a passenger in the flight, function 
{                                                     // searches for the name and deletes if it is found.
	printf("\n");
	if(start3 == NULL)
	{
		printf("\n");
		printf("Sorry, the passenger list is empty\n");
	}
	else
	{
		char name[80];
		printf("Please enter the name you want to delete\n");
		fflush(stdin);
		gets(name);
		passenger *count_node1, *count_node2;
		passenger *temp;
		for(count_node1 = start3; (count_node1 != NULL) && (strcmp(count_node1->name, name) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
		if(count_node1!= NULL)
		{
			if (count_node1 == start3)
			{
				temp = start3;
				start3 = start3->next;
				free(temp);
			}
			else if(count_node1->next == NULL && count_node1 != start3) 
			{
				temp = count_node1;
				count_node2->next = NULL;
				free (temp);
			}
			else
			{
				temp = count_node1;
				count_node2->next = count_node1->next;
				free (temp);
			}
		}
		else 
		{
			printf("\n");
			printf("Sorry, this name was not found in this list\n");
		} 
	}
	return start3;
}

passenger* passenger_menu(passenger *start3)                   // Menu Function which is called from main and takes 
{                                                              // over the program list after that              
	printf("\n");                                                     
	int choice;
	printf("Welcome to the passenger menu\n");
	printf("Please enter your choice from the menu shown below\n");
	printf("1: Add a passenger to this flight\n");
	printf("2: Delete a passenger from this flight\n");
	printf("3: Display the passenger list for this flight\n");
	printf("4: Return back to the Flight Menu\n");
	scanf ("%d", &choice);
	switch(choice)
	{
	case 1: 
		{ 
			start3 = create_passenger_node(start3);
		} break;
	case 2:
		{  
			start3 = delete_passenger_node(start3);
		} break;
	case 3:
		{
			print_passenger_list(start3);
		} break;
	case 4:
		{
			return start3;
		} break;
	default:
		{
			start3 = passenger_menu(start3);
		} break;
	}
	start3 = passenger_menu(start3);
	return start3;
}



void print_passenger_list(passenger *start3)
{
	printf("\n");
	if (start3 == NULL)
	{
		printf("Sorry, the passenger list is empty\n");
	}
	else
	{
		passenger *count;
		for (count = start3; count != NULL; count = count->next)
		printf("%s\n", count->name);
	}
}

//--------------------------------- MAIN FUNCTION CALLING THE MENU FUNCTION ------------------------------------------------

void main()
{
	start1 = read_from_file();
	start1 = airline_menu(start1);
} 
Last edited by LuciWiz : 02-Mar-2005 at 23:11. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 02-Mar-2005, 16:38
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi,

First thing first use code tags and not quotes to post your code.

Second thing, I could not even complie the program with gcc. It gave hella parser errors.

You are mixing C++ syntax with C which is not a good programming practice as you program will not be portable across older C compliers which do not understand C++ syntax.

You are creating ariline, flight and passenger structures on the top but when you are creating variables of these structure type you are missing the key word "strcuture" before them. For e.g.

CPP / C++ / C Code:
airline* create_airline_node(airline *start1); //Function Declarations


The above code line and similar such lines wouldnt even complie on ANCI C complier, unless you typedef your structure definition as a variable. The correct syntax should be

CPP / C++ / C Code:
struct airline * create_airline_node(struct airline *start1);

you need to "typedef" your structure as

CPP / C++ / C Code:
typedef struct airline airline;

Only after do this and similar things about your other structures, you code would complie.

You are not doing any error check while opening file for reading and writing. My first crash was at opening airline.txt. File was not there and fopen returned NULL. You need to check for NULL pointer before dereferencing it as it is a sure crash scenario.

I wish someone helps you out with your code. Its a really big code. Unless you focus on the area where you face problem and explain that in detail with whatever debugging you have done so far, it will be difficult for others to help you out.

Hope it helps.

Thanks,
  #3  
Old 02-Mar-2005, 18:56
batrsau batrsau is offline
New Member
 
Join Date: Mar 2005
Posts: 15
batrsau is on a distinguished road
The sad part is I am using a vc++ to compile this
I need some help in finding a compiler which can compile C code

can u help me with that a link and may be some installation instructions or if u could give me link to a good compiler for C that would be great help


Thanks for the help anyway I am going to keep looking to see where can i spot more bugs
  #4  
Old 02-Mar-2005, 21:03
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
If you complier can understand printf() stmts then it can very well understand C. The problem is not with complier you are using, but your coding style. You need to stick to either C or C++. Mixing the syntaxes is not a good idea. Your source code can then only be compiled with Compliers that understand both C and C++ syntax. Some older versions of GNU C compliers (prior to 3.0) wouldn't be able to complie it as they dont understand C++ coding style.

Runtime variable declarations all over the program code instaed at the begining of the function or as global, is another sign of such badly written C program. Although some latest version of C compiler allows this C++ style declarations and would throw any errors, its best to stick to standard C style for your program to be able to complier across any version of Complier.

I use gcc on linux.(mostly cause it comes free ). I am no expert on what complier you need to use but I guess for developing applications like these you can use a TurboC complier. It come with an IDE for editing your programs. I have been using it since my college days and its seems perfectly good choice for begineers to intermediate level programmers. It has a good online help as well for all the functions calls. You can google for it. I believe its free.
  #5  
Old 02-Mar-2005, 22:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Yeah, what nkhambal said. You are also using C code that is dangerous (gets()) and undefined (fflush()). See this link for information on these two functions.

Quote:
Originally Posted by batrsau
and finally here is the enitre code, I hope is not too big to post
In my opinion, yes it's too big (and unformatted). If you know where the trouple seems to be, you can post the section of code in question. That way we don't have to search 862 lines looking for the loop you mention.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #6  
Old 03-Mar-2005, 04:31
batrsau batrsau is offline
New Member
 
Join Date: Mar 2005
Posts: 15
batrsau is on a distinguished road

Thank You !!!


Thank You all very much for this constructive criticism and I really mean it thank you, :-D

I am newbie trying to do some programming my self and learning it from books and trying to code, infact this is my first post in a language forum so I was a little lost abt it .

Anyways I am looking for more tips to improve my programming style and need some help with good project making and tips for that

Can anyone please help me with a link that a good project listings and something that tells me good coding styles
  #7  
Old 03-Mar-2005, 10:33
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
There are lots of tutorials on this forum which talks about coding in C and various other techniques for improving your program and makng it more efficient and error-safe. They can be found on tutorials homepage.

Following the link

http://www.gidforums.com/f-41.html

Good luck.

BTW, were you able to find out where the problem was in your code?
  #8  
Old 03-Mar-2005, 12:07
batrsau batrsau is offline
New Member
 
Join Date: Mar 2005
Posts: 15
batrsau is on a distinguished road

Newbie Help needed on TC 2.01v


hi !

No I am still working on that program thats why I installed TC, I am following ur advice first on improving my programmig style before anything else and the best way is to make it work on a basic compiler

I recently installed TC 2.01v by Borland

I am running windowx XP

it had a zipped file which i unzipped and to a folder called tctemp and then clicked a file called install.exe which installed the program on C:/TC

Now when i start the program I have to go to the directory C:/TC and type TC

I started typing a a simple program and it worked fine but I ran into a few problems :

1. when I use any data type like int or float int the main function
the program gives me an error " expression syntax in main function "

2. I have been looking for a tutorail for this compiler but have been unable to find it, can anybody help me with that too

3. there were also some more instructions on the installtion which were to right click on the dos window and when a menu pops up then click properties and then type the name of file in some "....bat" somewhere in the properties menu. But when i right click on the DOS window i never get this kind of window

I need help very bad at this , I losing faith, aaaghhhh !!!!!!!
  #9  
Old 03-Mar-2005, 13:29
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Quote:
1. when I use any data type like int or float int the main function
the program gives me an error " expression syntax in main function "

Thats strange. One reason could be that your TC installation wasn't successful or proper i would say. Or another reason could be that you have your variable declarations scattered all over inside your main(). This is C++ style of coding. TC compiler will not understand that and will give out error at the Compile. You need to declare all your variables at the begining of the program in main(). For e.g. Following is correct C style

CPP / C++ / C Code:
#include <stdio.h>
int main ()
{ 
  int i,j;
  int k;
  
  scanf("i = %d j = %d:",&i,&j);
  printf("Multiplying %d with %d\n", i,j);
  
  k = i*j;
   
  printf("Result: %d\n",k);
  return 0;
}

Following may not work on TC or older complier as its C++ style.

CPP / C++ / C Code:
#include <stdio.h>
int main ()
{ 
  int i,j;
    
  scanf("i = %d j = %d:",&i,&j);
  printf("Multiplying %d with %d\n", i,j);
  
  int k = i*j;   /* C++ style declarations */
   
  printf("Result: %d\n",k);
  return 0;
}


to find the help about a perticular function call in TC for eg. printf or scanf just take your cursor to that word and press ctrl+F1 or just F1. It will give you all the info about that function and how it works and all. Its just like man pages on *nix platform.
  #10  
Old 03-Mar-2005, 14:59
batrsau batrsau is offline
New Member
 
Join Date: Mar 2005
Posts: 15
batrsau is on a distinguished road
I think its a very old compiler because my program was very simple :

CPP / C++ / C Code:
#include<stdio.h>

main()
{
    int i;
    printf("Hello World\n");
    return 0;
}

1.But I have another question which might solve thi problem when I am
compiling in VC++ if hte save the file as .c will it still work

2. If the a .c file works if the file has features of c++ will it give me errors or will it go ahead and compile it like in the program above
Last edited by admin : 29-Feb-2008 at 08:57. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.