I still having trouble to get the output correct for this program here is the as
ID: 3621266 • Letter: I
Question
I still having trouble to get the output correct for this program here is the assignment and the solution code that I have wrote; and the output I'm not sure what else to do to get the output correct.The purpose of this program is to simulate the operation of a taxi company that
serves customers around town and to generate a report that summarizes the operation of the
company. For this assignment, the company only owns one cab. Initially, the cab parks at the
location (0, 0) with no passenger on board. It has full tank (20 gallons) of fuel and its gas
mileage is 30 mpg without any passenger (the driver only). Each on board passenger will reduce
the gas mileage by 5%. For example the gas mileage changes as the following table.
Number of passenger Gas mileage (mpg)
0 30.0
1 30*95%=28.5
2 30*90%=27.0
3 30*85%=25.5
4 30*80%=24.0
5 30*75%=22.5
Whenever there is a new customer request and the cab is not serving any customer request, then
the cab will be dispatched to the customer’s location to pick up some passengers and drive them
to the destination. Before driving to any location, the cab will fill the gas tank to its capacity if it
finds it cannot reach the destination with the remaining fuel – i.e., it will compute the amount of
fuel needed to drive to the destination, and it will fill up the gas tank if it doesn’t have enough
gas.
The cab’s location, customer’s location and customer’s destination are denoted by x- and ycoordinates.
In this program, customer requests are stored in an input file (eg, “request.dat”). You should
read the input file name interactively and check for the file existence. The cab is required to
serve customer requests in the same order as they are listed in the data file. Each line of the data
file has the following information in the given order:
customer_id x-origin y-origin x-destination y-destination number-of-passengers
Output:
• Print information for the following activities. All information should be clear and
accurate.
o receiving a customer request, print customer id, # of passengers, location of
customers, and destination of customers
o passenger loading: print the number of customer on board
o passenger unloading: print the number of customers unloaded
o whenever the cab moves one location to another location: print the following
information
?? gas consumed during the trip
?? miles of the trip
?? gas remained after the trip
?? total miles traveled so far after the trip
?? total gas consumed so far after the trip
Assumption:
• The company operates one cab.
• Initially all cabs are parked at the location (0, 0) with no passenger on board. It has full
tank (20 gallons) of fuel.
• Each customer request has up to 5 passengers.
• One tank of gas is enough for the cab to drive from the current location to the customer’s
location, or from the customer’s location to customer’s destination.
• x- and y-coordinates are integers.
• The cab will not go back to the company’s location (0,0) after serving a customer request.
There are enough gas stations scattered throughout the district so that the driving distance (and gas consumed) from anywhere in the area to a gas station can be omitted during the calculation.
Class Design:
A cab used in this program is an instantiation of Vehicle class. Use the following class diagram that defines this class (You may add more methods and/or data members as needed.).
Vehicle
- passenger: integer
- remainingFuel: double
- totalMilesTraveled: double
- totalFuelConsumed: double
- x: int
- y: int
+ Vehicle()
+ pickupCustomer(int, int, int)
+ dropoffCustomer(int, int)
- moveTo (int, int)
- loadPassenger (int)
- unloadPassenger ()
- fillUpGas ()
Notes: + means public member and – means private member
This is the data file:
Customer1 12 24 60 80 3
Customer2 35 26 57 65 2
Customer3 99 10 10 100 4
Customer4 300 300 150 300 1
Customer5 350 200 500 100 2
Customer6 500 100 250 200 5
Customer7 234 123 678 123 3
Customer8 0 0 100 138 4
Customer9 100 600 200 300 5
Customer10 125 125 567 567 2
Customer11 400 400 410 410 5
Customer12 0 234 234 0 3
Customer13 345 0 0 123 1
Customer14 123 345 345 123 2
Customer15 78 33 26 70 5
here is the code
#ifndef Vehicle_H
#define Vehicle_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
class Vehicle
{
public:
Vehicle();
Vehicle(int , int , double , double );
void pickupCustomer();
void dropoffCustomer();
void PrintRequest();
void RequestProccesing(string , int ,int ,int ,int , int );
private:
int passenger;
int xlocation;
int ylocation;
int numpassengers;
double remainingFuel;
int xdistance;
int ydistance;
double needGas;
string customerid;
double totalMilesTraveled;
double totalFuelConsumed;
void moveTo(int,int,int,double);
void loadPassenger();
void unloadPassenger();
void fillUpGas();
};
#endif
#include "Vehicle.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
Vehicle::Vehicle(int x, int y, double rf, double tf)
{
xlocation= x;
ylocation= y;
remainingFuel= rf;
totalFuelConsumed= tf;
}
void Vehicle::RequestProccesing(string Id, int Clx,int Cly,int Xd,int Yd, int np)
{
customerid= Id;
xlocation= Clx;
ylocation= Cly;
xdistance= Xd;
ydistance= Yd;
numpassengers= np;
}
void Vehicle::PrintRequest()
{
cout<<endl;
cout<<"Request for customer: "<<customerid<<endl;
cout<<numpassengers<<"passengers meet to pick up at location "<<xdistance<<" "<<ydistance<<"."<<endl;
cout<<"Customer destination is: "<<xdistance<<" "<<ydistance<<endl<<endl;
}
void Vehicle::pickupCustomer()
{
double NeedGas= 0;
int tripdistance= 0;
if (xdistance >= xlocation && ydistance >= ylocation)
tripdistance= (xdistance-xlocation)+(ydistance- ylocation);
if (xdistance <= xlocation && ydistance >= ylocation)
tripdistance= (xdistance-xlocation)+(ydistance- ylocation);
if (xdistance >= xlocation && ydistance <= ylocation)
tripdistance= (xdistance-xlocation)+(ydistance- ylocation);
if (xdistance <= xlocation && ydistance <= ylocation)
tripdistance= (xdistance-xlocation)+(ydistance - ylocation);
if (tripdistance < 0)
tripdistance = tripdistance * -1;
NeedGas = tripdistance / 30;
if (remainingFuel > NeedGas)
{
cout<<"pick up trip: "<<endl;
moveTo(xlocation,ylocation, tripdistance, needGas);
loadPassenger();
}
else
{
cout<<"pick up trip: "<<endl;
fillUpGas();
moveTo(xlocation,ylocation, tripdistance, needGas);
loadPassenger();
}
}
void Vehicle::dropoffCustomer()
{
double NeedGas= 0;
int tripdistance =0;
double mpg= 0;
if(passenger==1)
mpg= 28.5;
else if(passenger==2)
mpg= 27.5;
else if(passenger==3)
mpg= 25.5;
else if (passenger==4)
mpg= 24.0;
else if (passenger== 5)
mpg= 22.5;
if (xdistance >= xlocation && ydistance >= ylocation)
tripdistance= (xdistance- xlocation)+(ydistance- ylocation);
if (xdistance <= xlocation && ydistance >= ylocation)
tripdistance= (xdistance- xlocation)+(ydistance- ylocation);
if (xdistance >= xlocation && ydistance <= ylocation)
tripdistance= (xdistance- xlocation)+(ydistance- ylocation);
if (xdistance <= xlocation && ydistance <= ylocation)
tripdistance= (xdistance- xlocation)+(ydistance- ylocation);
if (tripdistance < 0)
tripdistance= tripdistance * -1;
NeedGas= tripdistance /mpg;
if(remainingFuel > NeedGas)
{
cout<<"Delivery Trip: "<<endl;
moveTo(xdistance, ydistance, tripdistance, NeedGas);
}
else
{
cout<<"Delivery Trip: "<<endl;
fillUpGas();
moveTo(xdistance, ydistance, tripdistance, NeedGas);
}
}
void Vehicle::moveTo(int x, int y, int i, double G)
{
xlocation =x;
ylocation= y;
remainingFuel= G;
remainingFuel= remainingFuel - G;
totalMilesTraveled= totalMilesTraveled + i;
totalFuelConsumed= totalFuelConsumed + G;
cout<<"Miles traveled in this trip: "<<i<<endl;
cout<<"Total miles traveled: "<<totalMilesTraveled<<endl;
cout<<"Fuel used for trip: "<<setprecision(2)<<fixed<<G<<endl;
cout<<"Total fuel was used: "<<totalFuelConsumed<<endl;
cout<<"Remaining fule: "<<remainingFuel<<endl<<endl;
}
void Vehicle::loadPassenger()
{
passenger= numpassengers;
}
void Vehicle::unloadPassenger()
{
passenger= 0;
}
void Vehicle::fillUpGas()
{
remainingFuel= 20.0;
}
#include "Vehicle.h"
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
ifstream Infile;
string fileName;
string customerid;
int xlocation;
int ylocation;
int xdestination;
int ydestination;
int numpassengers;
Vehicle Cab(0, 0, 20.0, 0);
cout<<"Enter File Name:";
cin>>fileName;
Infile.open(fileName.c_str());
while(!Infile)
{
cout<<"File could not open"<<endl;
cout<<"Enter file again:";
cin>> fileName;
Infile.open(fileName.c_str());
}
Infile>> customerid>> xlocation>> ylocation>> xdestination>> ydestination>> numpassengers;
while(Infile)
{
Cab.pickupCustomer(customerid, xlocation, ylocation);
Cab.PrintRequest();
//Cab.pickupCustomer();
Cab.dropoffCustomer();
Infile>>customerid>>xlocation >>ylocation>> xdestination>> ydestination>> numpassengers;
}
return 0;
}
and here is the output that I'm getting:
Enter File Name:request.dat
Request for customer: Customer1
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer2
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer3
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer4
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer5
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer6
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer7
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer8
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer9
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer10
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer11
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer12
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer13
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer14
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Request for customer: Customer15
134515954passengers meet to pick up at location Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Customer destination is: 134523888 -135227724
Delivery Trip:
Total miles traveled: 2.18846e-314
Total fuel was used: 0
Remaining fule: 20
Explanation / Answer
i think i may know how you are getting some weird results it looks like you are trying to calculate the distance wrong tripdistance= (xdistance- xlocation)+(ydistance- ylocation); making sure i understand this right, if start location was (1,1) and end was (4,4) you are trying to add 3+3? Are you sure you are supposed to be headed in blocks and not in a straight line? a straight line from A to B working in cordinate systems is the Pythagorean Theorum, A^2 + B^2 = C^2 making your tripdistance tripdistance = (xdistance^2 + ydistance^2)^.5 this number will also always be positive, as squaring a negative number (i.e X1 is 1 and X2 is 8, (-7)^2 is 49) however even if you are moving in blocks and not a line, it doesn't seem that xdistance is ever initialized. you have void Vehicle::RequestProccesing(string Id, int Clx,int Cly,int Xd,int Yd, int np) { customerid= Id; xlocation= Clx; ylocation= Cly; xdistance= Xd; ydistance= Yd; . . . } but it never seems to be called? If it is called, i don't seem to see it. Thus you have junk data in xdistance when you try to use it and gives you erroneous results. Same thing with total miles traveled. Now this one i KNOW ever was initialized. you have double totalMilesTraveled; and then later in your moveTo function totalMilesTraveled= totalMilesTraveled + i; you are trying to add something to itself which was never initialized. junkdata = junkdata + int gives you junk back. As a rule of thumb, i always initialize my variables to something to avoid this. As for your fuel you have in your moveTo function remainingFuel= G; followed immediately by remainingFuel= remainingFuel - G; i don't think this was intended, as you are basically saying remainingFuel = G - G = 0 all the time? Start with that and see how it goes from there. You need to fix your calculations that rely on other calculations.