Making Program Compatible

I currently have a main.cc and a database.cc file that work perfectly, but my main.cc file does no match the one that is going to be tested. I've tried to convert it but it keeps throwing errors again and I don't have the time to break my whole code and start again.

This is my database.cc file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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;
	void print(std::ostream& os);
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


Passenger::Passenger(string first, string last, string dest)
{
 	fname = first;
 	lname = last;
 	destination = dest;
}

bool Passenger::operator==(const Passenger& p) const
{
	return fname == p.fname && lname == p.lname;
}

bool Passenger::operator<(const Passenger& p) const
{
	return fname < p.fname || (fname == p.fname && lname < p.lname);
}

void Passenger::print(std::ostream& os)
{
	os << fname << ' ' << lname << ' ' << destination << '\n';
}

int Flightlist::menu()
{
	int option;
	cout << endl;
	cout << "Enter one of the following options:" << endl;
	cout << "1. load reservations from file:" << endl;
	cout << "2. reserve a ticket" << endl;
	cout << "3. cancel a reservation" << endl;
	cout << "4. check reservation" << endl;
	cout << "5. display passenger list" << endl; 
	cout << "6. save passenger list" << endl;
	cout << "7. exit" << endl << endl;
	cin >> option;
	cin.get();
	return option;
}

void Flightlist::read_from_file(string filename)
{
	string fname, lname, destination;
	std::ifstream input(filename.c_str());
	while (input >> fname >> lname >> destination) 
	{					
		flist.push_back(Passenger(fname, lname, destination));
	}
	input.close();
}

void Flightlist::insert(Passenger p)
{
	flist.push_back(p);
}

void Flightlist::remove(Passenger p)
{
	flist.remove(p);
}

bool Flightlist::check_reservation(Passenger p)
{
	list<Passenger>::iterator i1, i2;
	i1 = flist.begin();
	i2 = flist.end();
	return flist.end() != find(flist.begin(), flist.end(), p);
}

void Flightlist::display_list()
{
	flist.sort();
	list<Passenger>::iterator i1, i2;
	i1 = flist.begin();
	i2 = flist.end();
	for ( ; i1 != i2; ++i1) {
		i1->print(cout);
	}
}

void Flightlist::save_to_file(string filename)
{
	flist.sort();
	list<Passenger>::iterator i1, i2;
	i1 = flist.begin();
	i2 = flist.end();
	std::ofstream output(filename.c_str());
	for ( ; i1 != i2; ++i1) {
		i1->print(output);
	}
	output.close();
}



This is my current main.cc file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "database.cc"

int main()
{	
	Flightlist flight_list;
	string fname, lname, destination;

	while (true) 
	{
		switch (flight_list.menu())
		{
			case 1:	
				{
					flight_list.read_from_file("ticket_reservations.dat");
					break;
				}

			case 2: 
				{
					cout << "first name of passenger:" << endl; 
					cin >> fname;
					cout << "last name of passenger" << endl;
					cin >> lname;
					cout << "destination of passenger" << endl;
					cin >> destination;
					flight_list.insert(Passenger(fname, lname, destination));
					break;
				}

			case 3: 
				{
					cout << "first name of passenger:" << endl; 
					cin >> fname;
					cout << "last name of passenger" << endl;
					cin >> lname;
					cout << "destination of passenger" << endl;
					cin >> destination;
					flight_list.remove(Passenger(fname, lname, destination));
					break;
				}

			case 4: 
				{
					cout << "first name of passenger:" << endl; 
					cin >> fname;
					cout << "last name of passenger" << endl;
					cin >> lname;
					cout << "destination of passenger" << endl;
					cin >> destination;
					if (flight_list.check_reservation(Passenger(fname, lname, destination)))
						cout << "this passenger has a ticket reservation" << endl;
					else
						cout << "this passenger does not have a ticket reservation" << endl;
					break;
				}

			case 5:	
				{
					flight_list.display_list();
					break;
				}

			case 6: 
				{
					flight_list.save_to_file("ticket_reservations.dat");
				}
				break;
			

			case 7:
				return 0;
		}
	}
	
	return 0;
}



This is the main.cc file that my program will be tested with, and therefore what I need to convert my code to be compatible with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "database.cc"

int main()
{
	list<Passenger> flight_list;
	string first_name, last_name, destination;

	while (true) 
	{
		switch (menu())
		{
			case 1:	
				{
					read_from_file(flight_list, "ticket_reservations.dat");
					break;
				}

			case 2: 
				{
					cout << "name of passenger:" << endl; 
					cin >> first_name >> last_name;
					cout << "destination:" << endl;
					cin.ignore();
					getline(cin, destination);
					insert(flight_list, first_name, last_name, destination);
					break;
				}

			case 3: 
				{
					cout << "name of passenger:" << endl;
					cin >> first_name >> last_name;
					remove(flight_list, first_name, last_name);
					break;
				}

			case 4: 
				{
					cout << "name of passenger:" << endl;
					cin >> first_name >> last_name;
					if (check_reservation(flight_list, first_name, last_name))
						cout << "this passenger has a ticket reservation" << endl;
					else
						cout << "this passenger does not have a ticket reservation" << endl;
					break;
				}

			case 5:	
				{
					display_list(flight_list);
					break;
				}

			case 6: 
				{
					save_to_file(flight_list, "ticket_reservations.dat");
				}
				break;

			case 7:
				return 0;
		}
	}
	
	return 0;
}


I desperately need help with this, thank you!
Last edited on
> what I need to convert my code to be compatible with:
¿and? ¿what do you need to do? ¿how is your code "not compatible"?


> #include "database.cc"
you shouldn't include source files.
@ne555 I define a Flightlist object in the main file and work everything through that, as well as using the Passenger object in every function in the main file. None of this is compatible with the main.cc class that my program will be tested against :/
Last edited on
@ne555 and I know about the .cc file inclusion but that's how we were given the code to work with, I have no control over that part. I myself made a separate .h file to utilise, but was told it wasn't allowed and that it all had to be in the same implementation file
I see you took my advice from the other thread and make a separate Flightlist class. Your Flightlist class is very clean and well implemented.

What you did not make clear in the other thread was that you could not make changes to main.cc. Had I known that, I would not have suggested making Flightlist a separate class.

You're going to have to go back to the simple functions you had in the previous thread.
I see no way you can utilize the Flightlist class if you have to use the provided main.cc.
Topic archived. No new replies allowed.