I have this program almost done, but there are quite a few errors. Can someone p
ID: 3570328 • Letter: I
Question
I have this program almost done, but there are quite a few errors. Can someone please help me fix them? It's for c++. Thank you!!
#include <iostream>
#include <string>
using namespace std;
class TravelInfo
{
string DestinationName;
int TravelDays;
double TripCost;
void setDestinationName(string Destination)
{
DestinationName = Destination;
}
string getDestinationName()
{
return DestinationName;
}
void setTravelDays(int Days)
{
TravelDays = Days;
}
int getTravelDays()
{
return TravelDays;
}
void setTripCost(double Cost)
{
TripCost = Cost;
}
double getTripCost()
{
return TripCost;
}
void operator << (TravelInfo& ob)
{
cout<<"TripCost = "<< ob.TripCost<< endl<< "TravelDays = "<< ob.TravelDays<< endl << "DestinationName = " << ob.DestinationName <<endl;
}
void add_travelDays(int days)
{
TravelDays += days;
TripCost += (days*((TripCost)/(TravelDays)));
}
void add_hotel()
{
cout << "What kind of hotel do you want to stay in? 1) Economy($70/day) 2)Luxury ($180$/day) and 3) Elite ($320/day)";
int ch;
cin >> ch;
switch(ch)
{
case 1:
TripCost += (TravelDays*70);
break;
case 2:
TripCost += (TravelDays*180);
break;
case 3:
TripCost += (TravelDays*320);
break;
default:
cout << "Invalid Choice";
}
}
void rent_a_car( int rentDays)
{
TripCost += (TravelDays*40);
}
friend void CompareTrip(const TravelInfo& tour1 , const TravelInfo& tour2)
{
if(tour1.TripCost > tour2.TripCost)
{
cout <<"The second tour is costlier."<<endl;
}
else if(tour1.TripCost < tour2.TripCost)
{
cout <<"The first tour is costlier."<<endl;
}
else
{
cout << "They both cost the same.";
}
}
TravelInfo()
{
TripCost = 0;
TravelDays = 0;
DestinationName = "";
}
TravelInfo(int days, int cost, string name)
{
TripCost = cost;
TravelDays = days;
DestinationName = name;
}
};
class PassengerDetails
{
string PassengerName;
string Destination;
char Gender;
};
bool checkAvailablity(int size,int NumOfPassengers, int MAX_SIZE)
{
if(size + NumOfPassengers < MAX_SIZE)
{
return true;
}
return false;
}
int main()
{
ifstream in_file;
in_file.open("FileNameXYZ.txt");
if (in_file.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
int a[20];
int stuff;
int i = 0;
while(in_file >> stuff)
{
a[i] = stuff;
i++;
}
TravelInfo ob1 = new TravelInfo();
TravelInfo ob2 = new TravelInfo();
TravelInfo ob3 = new TravelInfo();
TravelInfo ob4 = new TravelInfo();
ob1.setDestinationName(a[0]);
ob1.setTravelDays(a[1]);
ob1.setTripCost(a[2]);
ob1.setDestinationName(a[3]);
ob1.setTravelDays(a[4]);
ob1.setTripCost(a[5]);
ob1.setDestinationName(a[6]);
ob1.setTravelDays(a[7]);
ob1.setTripCost(a[8]);
ob1.setDestinationName(a[9]);
ob1.setTravelDays(a[10]);
ob1.setTripCost(a[11]);
PassengerDetails passengers[20];
int choice;
do{
cout<<"1-To view all the available tour packages"<<endl;
//Display the information of all the available tour packages using the overloaded <<
cout<<"2-To compare two different tour packages"<<endl;
//Take the input of two different destinations, and call the friend function CompareTrip() with
//appropriate objects as arguments
cout<<"3-Proceed to booking for an available package"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<obj1;
cout<<obj2;
cout<<obj3;
cout<<obj4;
break;
case 2:
string a,b;
cout<<"Enter destination 1?"<<endl;
cin>>a;
cout<<"Enter destination 2?"<<endl;
cin>>b;
TravelInfo tmp1,tmp2;
if(obj1.DestinationName == a)
tmp1 = obj1;
else if(obj2.DestinationName == a)
tmp1 = obj2;
else if(obj3.DestinationNmae == a)
tmp1 = obj3;
else if(obj4.DestinationName == a)
tmp1 = obj4;
if(obj1.DestinationName == b)
tmp2 = obj1;
else if(obj2.DestinationName == b)
tmp2 = obj2;
else if(obj3.DestinationNmae == b)
tmp2 = obj3;
else if(obj4.DestinationName == b)
tmp2 = obj4;
CompareTrip(tmp1,tmp2);
}
Explanation / Answer
Made few changes to remove all the errors
1) created a copy constructor
2) changed overloading of << a bit
3) parameter is changed from int to string in setDestinationName() call
4) completed do - while loop
_________________________________________________________________________________________
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class TravelInfo
{
public:
string DestinationName;
int TravelDays;
double TripCost;
TravelInfo(TravelInfo &t){
DestinationName = t.DestinationName;
TravelDays = t.TravelDays;
TripCost = t.TripCost;
}
void setDestinationName(string Destination)
{
DestinationName = Destination;
}
string getDestinationName()
{
return DestinationName;
}
void setTravelDays(int Days)
{
TravelDays = Days;
}
int getTravelDays()
{
return TravelDays;
}
void setTripCost(double Cost)
{
TripCost = Cost;
}
double getTripCost()
{
return TripCost;
}
friend void operator << (ostream &out, TravelInfo& ob)
{
out << "TripCost = " << ob.TripCost << endl << "TravelDays = " << ob.TravelDays << endl << "DestinationName = " << ob.DestinationName << endl;
}
void add_travelDays(int days)
{
TravelDays += days;
TripCost += (days*((TripCost) / (TravelDays)));
}
void add_hotel()
{
cout << "What kind of hotel do you want to stay in? 1) Economy($70/day) 2)Luxury ($180$/day) and 3) Elite ($320/day)";
int ch;
cin >> ch;
switch (ch)
{
case 1:
TripCost += (TravelDays * 70);
break;
case 2:
TripCost += (TravelDays * 180);
break;
case 3:
TripCost += (TravelDays * 320);
break;
default:
cout << "Invalid Choice";
}
}
void rent_a_car(int rentDays)
{
TripCost += (TravelDays * 40);
}
friend void CompareTrip(const TravelInfo& tour1, const TravelInfo& tour2)
{
if (tour1.TripCost > tour2.TripCost)
{
cout << "The second tour is costlier." << endl;
}
else if (tour1.TripCost < tour2.TripCost)
{
cout << "The first tour is costlier." << endl;
}
else
{
cout << "They both cost the same.";
}
}
TravelInfo()
{
TripCost = 0;
TravelDays = 0;
DestinationName = "";
}
TravelInfo(int days, int cost, string name)
{
TripCost = cost;
TravelDays = days;
DestinationName = name;
}
};
class PassengerDetails
{
string PassengerName;
string Destination;
char Gender;
};
bool checkAvailablity(int size, int NumOfPassengers, int MAX_SIZE)
{
if (size + NumOfPassengers < MAX_SIZE)
{
return true;
}
return false;
}
int main()
{
ifstream in_file;
in_file.open("FileNameXYZ.txt");
if (in_file.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
int a[20];
int stuff;
int i = 0;
while (in_file >> stuff)
{
a[i] = stuff;
i++;
}
TravelInfo ob1;
TravelInfo ob2;
TravelInfo ob3;
TravelInfo ob4;
string tstr;
stringstream ss1, ss2, ss3, ss4;
ss1 << a[0];
ss1 >> tstr;
ob1.setDestinationName(tstr);
ob1.setTravelDays(a[1]);
ob1.setTripCost(a[2]);
ss2 << a[3];
ss2 >> tstr;
ob2.setDestinationName(tstr);
ob2.setTravelDays(a[4]);
ob2.setTripCost(a[5]);
ss3 << a[6];
ss3 >> tstr;
ob3.setDestinationName(tstr);
ob3.setTravelDays(a[7]);
ob3.setTripCost(a[8]);
ss4 << a[9];
ss4 >> tstr;
ob4.setDestinationName(tstr);
ob4.setTravelDays(a[10]);
ob4.setTripCost(a[11]);
PassengerDetails passengers[20];
int choice;
do{
cout << "1-To view all the available tour packages" << endl;
//Display the information of all the available tour packages using the overloaded <<
cout << "2-To compare two different tour packages" << endl;
//Take the input of two different destinations, and call the friend function CompareTrip() with
//appropriate objects as arguments
cout << "3-Proceed to booking for an available package" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << ob1;
cout << ob2;
cout << ob3;
cout << ob4;
break;
case 2:
string a, b;
cout << "Enter destination 1?" << endl;
cin >> a;
cout << "Enter destination 2?" << endl;
cin >> b;
TravelInfo tmp1, tmp2;
if (ob1.DestinationName == a)
tmp1 = ob1;
else if (ob2.DestinationName == a)
tmp1 = ob2;
else if (ob3.DestinationName == a)
tmp1 = ob3;
else if (ob4.DestinationName == a)
tmp1 = ob4;
if (ob1.DestinationName == b)
tmp2 = ob1;
else if (ob2.DestinationName == b)
tmp2 = ob2;
else if (ob3.DestinationName == b)
tmp2 = ob3;
else if (ob4.DestinationName == b)
tmp2 = ob4;
CompareTrip(tmp1, tmp2);
}
} while (choice == 3);
cout << "Booking is Done" << endl;
return 0;
}