Please need help with C++ pointer program. Please and thank you. For this progra
ID: 664325 • Letter: P
Question
Please need help with C++ pointer program. Please and thank you.
For this program you'll be writing a class with dynamically allocated data. This is useful in situations where we don’t know at compile time how much memory we’ll need; either it may become very large, or it may not be needed at all.
Consider an airline’s database of customers. Most of its “regular” customers will only fly once or twice a year; on any given day, fewer than 2% of its customers have reservations to fly. Thus, there is little point in setting aside room in the database to store the reservation with the customer record. On the other hand, there are some people who fly weekly, and may have reservations made for several trips. So the number of reservations that need to be stored, while usually 0, is potentially larger. This is a classic situation in which dynamic allocation is needed; there’s no obviously correct size for the record that we can determine at compile time.
For this program you’ll be consolidating data from two input files into a single output file. The first file lists customers. For each customer there’s an identifier, followed by a first and last name. All of the data in this file should be treated as strings. The second file contains reservation information. Your output file will be a file listing only the passengers who have reservations, identifying the frequent flyers in the group.
Make a Passenger class consisting of an identifier (string) and first and last names, and a variable number of reservations.
A reservation is a struct consisting of a date, flight number, and departure and arrival airports (all strings). Define this struct in the header file for the Passenger class. Also define overloaded operators for input and output; the input operator assumes all items are separated by whitespace, and the output operator prints all items for the reservation, in the order given above, on one line, with a tab character (‘ ’) between items.
The Passenger class should have the following methods:
• A constructor that can take 0 parameters. This can be a separate constructor, or a constructor with default values for all parameters.
• A constructor that takes the passenger’s identifier and first and last names.
• Because this class uses dynamic memory, the class will need an overloaded assignment operator, a copy constructor, and a destructor. • Getters and setters for the ID, first name, last name, and a GetFullName() function. Getters should all be const functions, of course.
• A void AddReservation() method that takes a reservation, passed by const reference, and adds it to the list of reserations unless there is already a reservation for the same flight on the same date, in which case the reservation is not added; there is no error message in this case.
• A void ListReservations() method that takes an ostream passed by reference and prints a list of the reservations for that passenger. • A bool HasReservation() method that takes a flight number and date (both strings) and returns true if the Passenger has a reservation for the specified flight on the specified date, false otherwise. Your program has 2 input files: • Passengers.txt. This file contains (oddly enough) passengers, giving their identifiers and first and last names, all separated by whitespace. There is no reservation information in this file.
• Reservations.txt. This file contains the list of flight reservation. Each line begins with a passenger ID, then a Reservation as defined above, formatted so it can be read in with the input operator. For the output file, list only those passengers holding reservations. This file should be sorted by passenger last name, with ties broken on passenger first name. (Hint: Overload comparison operators for the Passenger class to handle this sort.) Passengers with 5 or more reservations should be flagged by printing the line: ***FREQUENT FLYER*** before the passenger information. Print at least one line blank space, or a clear dividing marker of some type, between each passenger’s information. Development notes:
• Your main program should store passenger information in a dynamic array. This means your main program should have a pointer to Passenger as a variable, not an array of Passengers. When you open the file for input, start by allocating a small array, maybe 5 to 10 items. As the array fills, you will need to re-allocate a new dynamic array and copy data into it. (An old programmer’s rule of thumb is that if a dynamic data structure fills up, make the new one twice as big as the old one.)
• Read all the passengers into a single array, and then search the array as needed to find the passenger with a given ID number. (This may or may not be the most efficient way to solve the problem, but the point of the assignment is to give you practice with overloading operators, dynamic memory, and searching and sorting.)
• Your program will be tested against a different data file with the same format.
Developer notes: Passenger.txt has a ton of ID numbers containing 20 numbers with a tab space then Last name,Firstname.
EX:03444249137123814150 Jacobs, Britney
Reservations.txt has 27 reservations with ID numbers containg 20 number with a tab space then the date tab then another 3 number ID number then another tab and 3 letter airport and another tab then 3 letter airport.
EX: 33135695988395136288 08/25/2016 126 LTL RTX
Here is dropbox with the input.txt files:
https://www.dropbox.com/sh/36sxpfkktf7rawv/AABiPMaEmnPVSsVlR65Jr7S6a?dl=0
Explanation / Answer
Please need help with C++ pointer program. Please and thank you.
For this program you'll be writing a class with dynamically allocated data. This is useful in situations where we don’t know at compile time how much memory we’ll need; either it may become very large, or it may not be needed at all.
Consider an airline’s database of customers. Most of its “regular” customers will only fly once or twice a year; on any given day, fewer than 2% of its customers have reservations to fly. Thus, there is little point in setting aside room in the database to store the reservation with the customer record. On the other hand, there are some people who fly weekly, and may have reservations made for several trips. So the number of reservations that need to be stored, while usually 0, is potentially larger. This is a classic situation in which dynamic allocation is needed; there’s no obviously correct size for the record that we can determine at compile time.
For this program you’ll be consolidating data from two input files into a single output file. The first file lists customers. For each customer there’s an identifier, followed by a first and last name. All of the data in this file should be treated as strings. The second file contains reservation information. Your output file will be a file listing only the passengers who have reservations, identifying the frequent flyers in the group.
Make a Passenger class consisting of an identifier (string) and first and last names, and a variable number of reservations.
A reservation is a struct consisting of a date, flight number, and departure and arrival airports (all strings). Define this struct in the header file for the Passenger class. Also define overloaded operators for input and output; the input operator assumes all items are separated by whitespace, and the output operator prints all items for the reservation, in the order given above, on one line, with a tab character (‘ ’) between items.
The Passenger class should have the following methods:
• A constructor that can take 0 parameters. This can be a separate constructor, or a constructor with default values for all parameters.
• A constructor that takes the passenger’s identifier and first and last names.
• Because this class uses dynamic memory, the class will need an overloaded assignment operator, a copy constructor, and a destructor. • Getters and setters for the ID, first name, last name, and a GetFullName() function. Getters should all be const functions, of course.
• A void AddReservation() method that takes a reservation, passed by const reference, and adds it to the list of reserations unless there is already a reservation for the same flight on the same date, in which case the reservation is not added; there is no error message in this case.
• A void ListReservations() method that takes an ostream passed by reference and prints a list of the reservations for that passenger. • A bool HasReservation() method that takes a flight number and date (both strings) and returns true if the Passenger has a reservation for the specified flight on the specified date, false otherwise. Your program has 2 input files: • Passengers.txt. This file contains (oddly enough) passengers, giving their identifiers and first and last names, all separated by whitespace. There is no reservation information in this file.
• Reservations.txt. This file contains the list of flight reservation. Each line begins with a passenger ID, then a Reservation as defined above, formatted so it can be read in with the input operator. For the output file, list only those passengers holding reservations. This file should be sorted by passenger last name, with ties broken on passenger first name. (Hint: Overload comparison operators for the Passenger class to handle this sort.) Passengers with 5 or more reservations should be flagged by printing the line: ***FREQUENT FLYER*** before the passenger information. Print at least one line blank space, or a clear dividing marker of some type, between each passenger’s information. Development notes:
• Your main program should store passenger information in a dynamic array. This means your main program should have a pointer to Passenger as a variable, not an array of Passengers. When you open the file for input, start by allocating a small array, maybe 5 to 10 items. As the array fills, you will need to re-allocate a new dynamic array and copy data into it. (An old programmer’s rule of thumb is that if a dynamic data structure fills up, make the new one twice as big as the old one.)
• Read all the passengers into a single array, and then search the array as needed to find the passenger with a given ID number. (This may or may not be the most efficient way to solve the problem, but the point of the assignment is to give you practice with overloading operators, dynamic memory, and searching and sorting.)
#include <iostream>
#include <fstream>
using namespace std;
class Passengers
{
private String passsengerid;
private String firstname;
private String lastname;
private int reservationNo;
Passengers();
~Passengers();
Passengers(String passid,String firstname,String lastname);
public:
void setpasssengerid( String pasid);
String getpasssengerid( void );
public:
void setfirstname( String fname);
String getfirstname( void );
public:
void setlastname( String lname);
String getlastname( void );
public:
String getFullName(String fname,String lname);
public:
void ListReservations();
boolean HasReservation();
void sortReservations();
void addPassenger(int pid,String fname,String lname,String rid);
struct reservation
{
String flightNo;
String arrtime;
String deptime;
};
};
void Passengers::setpasssengerid( String pid )
{
passsengerid= pid ;
}
String Passengers::getpasssengerid( void )
{
return passsengerid;
}
void Passengers::setfirstname( String fname )
{
firstname= fname ;
}
String Passengers::getfirstname( void )
{
return firstname;
}
void Passengers::setlastname( String lname )
{
lastname= lname ;
}
String Passengers::getlastname( void )
{
return lastname;
}
String Passengers::getFullName( void )
{
return firstname+" "+lastname;
}
Passengers::Passengers(void)
{
cout << "Object is being created" << endl;
}
Passengers::~Passengers(void)
{
cout << "Object is being deleted" << endl;
}
Passengers::Passengers(String passid,String fname,String lname)
{
passengerid=passid;
firstname=fname;
lastname=lname;
addPassenger(passid,fname,lname,rid);
}
void ListReservations()
{
ifstream inputfile;
ofstream outputfile;
char chs;
openinfile(inputfile);
outputfile.open("C: eservation.txt");
while (!inputfile.eof())
{
inputfile.get(chs);
if (!inputfile.eof())
{
cout<<chs;
outputfile<<chs;
}
}
cout<<" Reading and writing file is completed!"<<endl;
inputfile.close();
outputfile.close();
}
boolean HasReservation()
{
boolean result=false;
int countwords=0;
string path="reservations.txt';
string line;
string word;
ifstream File(path);
if(File.is_open())
{
while(getline(File,line))
{
cout<<line<<endl;
}
cout<<"Write the word you're searching for."<<endl;
cin>>word;
while(!File.eof())
{
if(word.compare(line)==0)
countwords++;
}
cout<<"The word has been found "<<countwords<<" times."<<endl;
File.close();
}
else
{
cout<<"Error! File not found!";
exit(1);
}
if(countwords>0)
{
result=true;
}
}
void sortReservation()
{
double * s;
int ns = 10; //information
s = new double[ns];
for (int i = 0 ; i < ns ; ++i)
s[i] = 17*i + 23; // i put my information here
ofstream os("data.txt", ios::binary);
os.write(reinterpret_cast<const char*> (s), sizeof(double) * ns);
os.close();
double * final;
final = new double[ns];
ifstream is("reservation.txt", ios::binary);
is.read(reinterpret_cast<char *>(final), sizeof(double) * ns);
is.close();
for (int i=0; i < ns; ++i)
cout<<s[i]<<" "<<final[i]<<" ";
return 0;
}
void addPassengers(int pid,String fname,String lname,String rid)
{
std::ofstream outfile;
outfile.open("passenger.txt", std::ios_base::app);
outfile << pid <<fname <<lanme <<rid;
return 0;
}
}
#include Passenger.h
using namespace std;
class AirPassengers
{
private:
String passsengerid;
Stringfirstname;
String lastname;
int reservationNo;
public:
char *foo;
foo=new char[10];
};
int main()
{
Passenger p1=new Passenger("EWHN548","pandu","ravi",2231432);
p1.listReservation();
p1.sortResevation();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.