I am having problem with saving and retrieving information from a file, program saves info to a flight.txt file its in the file, but debugger breaks as soon as i am trying to display content
here's the code:

#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;
#define MAXAIRNAME 100

struct  FlightDetails
{  	
     string ID;
     char departure[50];
     char destination[50];
     char airlineName[50];
     //double adultPrice = 200;
     //double childPrice = 50;
     //double totalPrice = 0;
};
typedef struct FlightDetails F;

struct Flight
{    F flightRecord;
	 struct Flight *link;
};
typedef struct Flight node;

//FLIGHT DETAILS
node * createList(node * head, F flightRecord, node ** last);
node * createFlightNode(node * newNode, F flightRecord);
void displayList(node * head, string fFile);
//FLIGHT DETAILS

int main()
{	
   system ("CLS");
   
   char choice;
   string fFile = "flight.txt"; 
   node *head=NULL; 
   node *last=head; //initial value//&last is a double pointer
   F flightRec;
   do
    {                
              cout << "\n\n\n\n\n\n";
              cout << setw(40) << " Menu \n\n";
              cout << setw(40) << " 1:" << " Enter Flight Details:\n " << endl;
              cout << setw(40) << " 2:" << " Display Flight Details:\n " << endl;
              cout << setw(40) << " 3:" << " Exit\n " << endl;
              cin >> choice;  
              cin.get();
              
              //start of switch statement
              switch(choice)
             {
              case '1' : system("CLS");  //ENTER FLIGHT DETAILS
               {                         
                  ///// FLIGHT /////
				    node * createFlightNode(node * newNode, F flightRecord);
					{					
                         cout << "\n\n\n\n\n"; 		     
                         cout << setw(35) <<""<< "Flight Details: " << endl;
                         cout << endl << endl;
                                 
						 cout << setw(35) <<""<< "Enter Flight ID: " << " ";
						 cin  >> flightRec.ID;
						 cout << endl;
							     
						 while( flightRec.ID != "!")
			              {
			                  cout << setw(35) <<""<< "Enter Departure Airport: " << " ";
			                  cin.ignore();
			                  cin.getline(flightRec.departure, MAXAIRNAME -1);
                 			 //cin >> flightRec.departure;
			                  cout << endl;
							     
			                  cout << setw(35) <<""<< "Enter Destination Airport: " << " ";
			                  cin.ignore();
			                  cin.getline(flightRec.destination, MAXAIRNAME -1);
			                  cout << endl;
							     
			                  cout << setw(35) <<""<< "Enter Airline Title: " << " ";
			                  cin.ignore();
			                  cin.getline(flightRec.airlineName, MAXAIRNAME -1);
			                  cout << endl << endl;
			                  system("CLS");
								 
							  cout << setw(35) <<""<< "Enter - ! -  to exit:";
							  cout << endl;
							  cout << setw(35) <<""<< " or " << endl;
                              cout << endl;
                                                     
							  head=createList(head,flightRec,&last); 
							                     								                     
                              //this passes back the double pointer &last for the next position 
                              cout << setw(35) <<""<< "Enter Flight ID: " << " ";
                              cin >> flightRec.ID;
                              system("CLS");         
                          }//end while
				     }//end node
                   }//case A end
                   break; 
                   
                   case '2' : system("CLS");
                   {
                        displayList(head, fFile);
                        cout << endl;
                        system("pause");
                        system("CLS");
                   }//case B end
                   break;         
             }//end switch
       }//do end
   while (choice != '3'); 
}//int main end                                      
//function to create one node with details
node * createFlightNode(node * newNode, F flightRecord)
 {
    newNode = new node;
    newNode-> flightRecord = flightRecord;    
    newNode->link = NULL;
    
    return newNode;
 }
//adds one record at a time to the list, double pointer last returned with new last value
node *createList(node * head, F flightRec, node ** last)
 {  
   if( head == NULL)
    {        
     head = createFlightNode(head, flightRec);
     //the contents of the double pointer same as original last pointer
     *last = head; 
    }
   else 
    {      
     //(*last) refers to old last pointer		
     (*last)-> link = createFlightNode(  (*last)->link, flightRec);
     //*last refers to old last single pointer
     *last = (*last)->link;
    }
   return head;
 }
void displayList(node * head,string Ffilename)
{       
   node * current;
   current = head;
   cout << "\n\n" << endl;
   ofstream outfile;  
   outfile.open("flight.txt",ios::app | ios::out); 
   do 
    { 
    outfile << "Flight ID            :" << "  "<< current->flightRecord.ID << endl; 
    outfile << "Departure Airport    :" << "  "<< current->flightRecord.departure << endl;
    outfile << "Destination Airport  :" << "  "<< current->flightRecord.destination << endl;
    outfile << "Airline Name         :" << "  "<< current->flightRecord.airlineName << endl;
    current=current->link; 
    }//end do
    while(current!=NULL); 
    outfile << "END OF FILE"; 
    outfile.close();
    cout << "Save succesful"; 
}//end void

The program worked correctly for me, when I entered some flights and then chose the display.

But Display is wrongly named - it's a Save.

If you try to run the program and directly choose option 2, you are trying to save a list that has no nodes. Thus your program crashes, you're trying to access memory address 000000000.

Perhaps you need to include a check for head pointer being NULL, and not call the Display function in that case.

id say you are right, but what changes do i have to make? which part of code should i change exactly??? can you help me out with this please, coz i've been changing it around, but nothing happened.

well, i have made some changes. it seems to be working, but still number "2" in menu crashes...i think i am having problem with displaying content of the file. here's the code anyways, please have a look at it, and tell me what is wrong with it. thanks in advance

#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;
#define MAXAIRNAME 100

struct FlightDetails
{
       string ID;
       char departure[50];
       char destination[50];
       char airlineName[50];
};
typedef struct FlightDetails F;
 
struct Flight
{ 
       F flightRecord;
       struct Flight *link;
};
typedef struct Flight node;

//FLIGHT DETAILS
node * createList(node * head, F flightRecord, node ** last);
node * createFlightNode(node * newNode, F flightRecord);
void displayList(node * head, F flightRecord);
//FLIGHT DETAILS
 
int main()
{
system ("CLS");
char choice;
//string fFile = "flight.txt";
ifstream fFile ("flight.txt");
node *head=NULL;
node *last=head; //initial value//&last is a double pointer
F flightRecord;
  do
    {
            cout << "\n\n\n\n\n\n";
            cout << setw(40) << " Menu \n\n";
            cout << setw(40) << " 1:" << " Enter Flight Details:\n " << endl;
            cout << setw(40) << " 2:" << " Display Flight Details:\n " << endl;
            cout << setw(40) << " 3:" << " Exit\n " << endl;
            cin >> choice;
            cin.get();
//start of switch statement
           switch(choice)
            {
                  case '1' : system("CLS"); //ENTER FLIGHT DETAILS
                   {
                   ///// FLIGHT /////
                         node * createFlightNode(node * newNode, F flightRecord);
                          {
                              cout << "\n\n\n\n\n";
                              cout << setw(35) <<""<< "Flight Details: " << endl;
                              cout << endl << endl;
                              cout << setw(35) <<""<< "Enter Flight ID: " << " ";
                              cin >> flightRecord.ID;
                              cout << endl;
                             while( flightRecord.ID != "!")
                              {
                                 cout << setw(35) <<""<< "Enter Departure Airport: " << " ";
                                 cin.ignore();
                                 cin.getline(flightRecord.departure, MAXAIRNAME -1);
                                 cout << endl;
                                 cout << setw(35) <<""<< "Enter Destination Airport: " << " ";
                                 cin.ignore();
                                 cin.getline(flightRecord.destination, MAXAIRNAME -1);
                                 cout << endl;
                                 cout << setw(35) <<""<< "Enter Airline Title: " << " ";
                                 cin.ignore();
                                 cin.getline(flightRecord.airlineName, MAXAIRNAME -1);
                                 cout << endl << endl;
                                 system("CLS");
                                 cout << setw(35) <<""<< "Enter - ! - to exit:";
                                 cout << endl;
                                 cout << setw(35) <<""<< " or " << endl;
                                 cout << endl;
                                 
                                 head=createList(head,flightRecord,&last);
                                 //this passes back the double pointer &last for the next position
                                 cout << setw(35) <<""<< "Enter Flight ID: " << " ";
                                 cin >> flightRecord.ID;
                                 system("CLS");
                              }//end while
                          }//end node
                   }//case A end
                   break;

                  case '2' : system("CLS");
                   {
                    displayList(head, flightRecord);
                    cout << endl;
                    system("pause");
                    system("CLS");
                   }//case B end
                   break;
           }//end switch
   }//do end
 while (choice != '3');
}//int main end 

//function to create one node with details
node * createFlightNode(node * newNode, F flightRecord)
{
     newNode = new node;
     newNode-> flightRecord = flightRecord;
     newNode->link = NULL;
     return newNode;
}
//adds one record at a time to the list, double pointer last returned with new last value
node *createList(node * head, F flightRecord, node ** last)
 {
   if( head == NULL)
    {
     head = createFlightNode(head, flightRecord);
     //the contents of the double pointer same as original last pointer
     *last = head;
    }
   else
    {
     //(*last) refers to old last pointer
     (*last)-> link = createFlightNode( (*last)->link, flightRecord);
     //*last refers to old last single pointer
     *last = (*last)->link;
    }
    node * current;
    current = head;
    cout << "\n\n" << endl;
    ofstream outfile;
    outfile.open("flight.txt",ios::app | ios::out);
    do
     {
        outfile << "Flight ID :" << " "<< current->flightRecord.ID << endl;
        outfile << "Departure Airport :" << " "<< current->flightRecord.departure << endl;
        outfile << "Destination Airport :" << " "<< current->flightRecord.destination << endl;
        outfile << "Airline Name :" << " "<< current->flightRecord.airlineName << endl;
        current=current->link;
     }//end do
    while(current!=NULL);
        outfile << "END OF FILE";
        outfile.close();
    return head;
 }
 
void displayList(node * head, F flightRecord)
{
  node * current;
  current = head;
  //head==NULL;
  cout << "\n\n" << endl;
  ifstream fFile ("flight.txt");
  if (fFile.is_open())
  {
   while (! fFile.eof() )
    {
      //getline (fFile);
      cout << "Flight ID: " << current->flightRecord.ID <<endl;
      cout << "Departure Airport: "<< current->flightRecord.departure << endl;
      cout << "Destination Airport: " << current->flightRecord.destination << endl;
      cout << "Airline Name: " << current->flightRecord.airlineName << endl;
      cin.get();     
      system("pause"); 
    }
      fFile.close();
   }
  else
   {
      cout << "Unable to open file"; 
   }
}//end void

but still number "2" in menu crashes...i think i am having problem with displaying content of the file.

You are not reading from the file anything. So, it will likely turn out to be an infinite loop there.

You are not reading from the file anything. So, it will likely turn out to be an infinite loop there.

it reads for the first time, untill i close the program, but it repeats the information while reading it, it shows only one item...as you have mentioned it is a infinite loop. How would i change that?

it reads for the first time, untill i close the program, but it repeats the information while reading it, it shows only one item

displayList() really is not reading anything from the file. It only displays the values off the current list's head over and over again (if it manages to enter the loop).

>>How would i change that?
Actually, is it clear to you what this displayList() function is supposed to do? Is it only supposed to display the data stored in the file or perhaps something else?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.