I am new to C++ and am required to build an airline reservation system (seems to be a common assignment). I have written pseudo-code with the help of a tutor but would like to know if you could correct what I have so far and guide me to my next steps please (;
#include <iostream>
#include <vector>
class Record
{
private:
struct Flight
{
string FlightNo;
int seats;
}
struct Passenger
{
string Name;
string PhoneNumber;
string FlightNum;
string Date;
}
vector<Flight> FList;
vector<Passenger> PList;
public:
void NewFlight(string FN, int seats)
{
for(int i =0; i<FList.size(); i++)
{
if(FN.strcmp(FList[i].FlightNo))
{
cout << "Flight already exists" << endl;
return;
}
}
Flight Addition = new Flight; // create new struct
Addition.FlightNo = FN;
Addition.seats = seats;
FList.push_back(Addition);
return;
}
void add(string Name, string PhoneNumber, string FlightNum, string Date)
// go through passenger list to check name and make sure reservation does not exist
// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"
// otherwise, add object to passenger list
usingnamespace std;
int main()
{
}
usingnamespace std; should be above any uses of things in ::std, so put it just below your #includes.
Lines 11, 18, and 41 are missing semicolons and you are missing the ending }; to the class.
if(FN.strcmp(FList[i].FlightNo)) ...string doesn't have a member function called strcmp. Use the == operator instead (it's overloaded): if(FN == FList[i].FlightNo) ...
The return; is not necessary on line 38.
Change line 34 to Flight Addition;. There is no need for dynamic memory here.
#include <iostream>
#include <vector>
usingnamespace std;
class Record
{
private:
struct Flight
{
string FlightNo;
int seats;
};
struct Passenger
{
string Name;
string PhoneNumber;
string FlightNum;
string Date;
};
vector<Flight> FList;
vector<Passenger> PList;
public:
void NewFlight(string FN, int seats)
{
for(int i =0; i<FList.size(); i++)
{
if(FN == FList[i].FlightNo))
{
cout << "Flight already exists" << endl;
return;
}
}
Flight Addition;
Addition.FlightNo = FN;
Addition.seats = seats;
FList.push_back(Addition);
};
void add(string Name, string PhoneNumber, string FlightNum, string Date)
// go through passenger list to check name and make sure reservation does not exist
// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"
// otherwise, add object to passenger list
};
int main()
{
}
Thank you. I have revised the code again. I am now trying to allow the user to input the name of a file that contains flight numbers followed by the number of seats. I know/think this has something to do with reading a file and loading it into the program, but any guidance would be appreciated.
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
class ReservationRecords
{
private:
struct Flight
{
string FlightNo;
int seats;
};
struct Record
{
string Name;
string PhoneNumber;
string FlightNum;
string DateOfTravel;
};
vector<Flight> FList;
vector<Record> PList;
public:
// add all info into flightlist
// function {}
void NewFlight(string FN, int seats)
{
for(int i =0; i<FList.size(); i++)
{
if(FN.compare(FList[i].FlightNo))
{
cout << "Flight already exists" << endl;
return;
}
}
Flight Addition;
Addition.FlightNo = FN;
Addition.seats = seats;
FList.push_back(Addition);
};
void add(string Name, string PhoneNumber, string FlightNum, string Date)
{
// go through passenger list to check name and make sure reservation does not exist
// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"
// otherwise, add object to passenger list
};
};
int main()
{
// call build flight list
}
Thank you. That helped a little, but how would I prompt the user to enter a file name, then have that data read in to the program, then close the file?
#include <iostream>
#include <fstream> // stream class to both read and write from/to files
#include <string>
usingnamespace std;
int main () {
string line;
string filename;
cout << "Reservations>> ";
cin >> filename;
ifstream myfile (filename); // stream class to read from files
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl; // temporary to test if file is read.
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Okay. So this worked. Now my problem is that my input file has the following format:
DL1234 50
DL2134 84
DL0000 2
This is the flight number followed by the seat capacity. How do I go about reading each line, and parsing each data entry (i.e., flight number separated by seat capacity)?
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
usingnamespace std;
class ReservationRecords
{
private:
struct Flights
{
string FlightNumber;
int TotalSeats;
int TakenSeats;
};
struct Record
{
string Name;
string PhoneNumber;
string FlightNumber;
string DateOfTravel;
};
vector<Flights> FlightList;
vector<Record> RecordList;
public:
// add all info into flightlist
void GetFlightData() {
string filename;
cout << "Reservations>> ";
cin >> filename;
ifstream inputfile (filename); // ifstream = stream class to read from files
if (inputfile.is_open())
cout << "Flight numbers and seat availability uploaded\n";
else
cout << "ERROR: cannot open input file.\n";
// reading in flight number and seat capacity from "filename"
string FilenameFlightNumber;
int FilenameTotalSeats;
while (!inputfile.eof()) {// While not at end of inputfile
inputfile >> FilenameFlightNumber;
inputfile >> FilenameTotalSeats;
Flights AddToList;
AddToList.FlightNumber = FilenameFlightNumber;
AddToList.TotalSeats = FilenameTotalSeats;
FlightList.push_back(AddToList);
};
};
void NewFlight(string NewFlightNumber, int NewFlightTotalSeats) {
for(int i =0; i<FlightList.size(); i++)
{
if(NewFlightNumber.compare(FlightList[i].FlightNumber)) // compare compares if equal
{
cout << "A flight numbered " << NewFlightNumber << " already exists. Flight not created.\n";
return;
}
}
Flights AddToList;
AddToList.FlightNumber = NewFlightNumber;
AddToList.TotalSeats = NewFlightTotalSeats;
FlightList.push_back(AddToList);
};
void add(string AddName, string AddPhoneNumber, string AddFlightNumber, string AddDate) {
// go through passenger list to check name and make sure reservation does not exist
// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"
// otherwise, add object to passenger list
for(int i=0; i<RecordList.size(); i++)
{
if(AddName.compare(RecordList[i].Name) && AddFlightNumber.compare(RecordList[i].FlightNumber) && AddDate.compare(RecordList[i].DateOfTravel))
{
cout << "This reservation already exists.\n";
return;
}
elseif(AddFlightNumber.compare(RecordList[i].FlightNumber))
{ // we found the flight
// check if seats are available
int j;
for (j = 0; j < FlightList.size(); j++)
if (RecordList[i].FlightNumber == FlightList[j].FlightNumber)
if (FlightList[j].TotalSeats == FlightList[j].TakenSeats)
{
cout << "No seat available.\n";
return;
}
// seats are available and we can the record
Record AddToList;
AddToList.Name = AddName;
AddToList.PhoneNumber = AddPhoneNumber;
AddToList.FlightNumber = AddFlightNumber;
AddToList.DateOfTravel = AddDate;
RecordList.push_back(AddToList);
FlightList[j].TakenSeats++;
return;
}
}
// exit the loop
// we have not found a flight number
cout << "No flight numbered " << AddFlightNumber << " exists.\n";
};
// Delete record and subtract 1 from taken seats of that specific flight number.
// If record.name does not exist, print "This reservation does not exist."
void Delete(string DeleteName, string DeleteFlightNumber, string DeleteDate) {
for(int i=0; i<RecordList.size(); i++)
{
if(DeleteName.compare(RecordList[i].Name) && DeleteFlightNumber.compare(RecordList[i].FlightNumber) && DeleteDate.compare(RecordList[i].DateOfTravel))
{
// Delete record [i] and subtract 1 from Flights.TakenSeats
RecordList[i] = NULL;
FlightList[i].TakenSeats--;
return;
}
}
cout << "This reservation does not exist\n";
};
};
int main()
{
// call build flight list
};