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