I have numerous functions written to implement on a linked list of objects. All I have left to write is the display_list()
and the save_to_file()
functions, which are proving to be quite difficult for me. I currently have the code for how it is done with strings instead of Passenger objects, but none of my conversions work. Below are only the relevant parts of the files.
main.cc
case 5:
{
display_list();
break;
}
case 6:
{
save_to_file("ticket_reservations.dat");
break;
}
database.h
include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<fstream>
#ifndef passenger_h
#define passenger_h
using std::string;
using std::cin;
using std::cout;
using std::list;
using std::endl;
class Passenger {
public:
Passenger() {}
Passenger(string, string, string);
bool operator==(const Passenger&) const;
bool operator<(const Passenger&) const;
private:
string fname, lname, destination;
};
class Flightlist {
public:
int menu();
void read_from_file(string);
void insert(Passenger p);
void remove(Passenger p);
bool check_reservation(Passenger p);
void display_list();
void save_to_file(string)
private:
list<Passenger> flist;
};
#endif
database.cc
bool Flightlist::check_reservation(Passenger p) //example of working function
{
list<Passenger>::iterator i1, i2;
i1 = flist.begin();
i2 = flist.end();
return flist.end() != find(flist.begin(), flist.end(), p);
}
void display_list()
{
flist.sort();
list<Passenger>::iterator i1, i2;
i1 = flist.begin();
i2 = flist.end();
for ( ; i1 != i2; ++i1) {
cout << *i1 << endl;
}
}
void save_to_file(string filename)
{
flist.sort();
list<Passenger>::iterator i1, i2;
i1 = flist.begin();
i2 = flist.end();
ofstream output(filename.c_str());
for ( ; i1 != i2; ++i1) {
output << *i1 << " ";
}
output.close();
}
The sort should take from the overloaded operators (== and <) so that they are sorted lexicographically on the last name.
Any help would be very appreciated!