Yes I was wondering if you could help me out on how to add a while loop to this code so that it calls the pump request for pump one and two using a ramdon number generator giving numbers between 1 and 5. The loop should terminate after five requests. Here is the code. Thank you for looking at it.
Randy
#include <iostream.h>
#include <iomanip.h>
class Pump
{
private:
int position; //Pump one or two
float amtInTank; //Amount of gas in tank
float price; //Price of gas/gallon
float tankCapacity; //Gallons tank can hold
float galSold; //Number gallons sold
float totalSales; //Total price of gallons sold
public:
Pump(int = 1, float = 50.0, float = 1.00,
float = 100.0, float = 0, float = 0); // constructor
void current_status(void);
void request(float);
void addgas(float); //Add gas to tank
void newprice(float); //Change gas price
};
Pump:😛ump(int pump_number, float start, float todaysPrice,
float holding_total, float total_sales, float total_earnings)
{
position = pump_number;
amtInTank = start;
price = todaysPrice;
tankCapacity = holding_total;
galSold = total_sales;
totalSales = total_earnings;
}
void Pump::current_status(void)
{
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint)
<< setprecision(2);
cout << "The gas tank number " << position << " has " << amtInTank << " gallons of gas." << endl;
cout << "The price per gallon of gas is $" << price << endl;
return;
}
void Pump::request(float pumpAmt)
{
float pumped,
count;
if (amtInTank >= pumpAmt)
pumped = pumpAmt;
else
pumped = amtInTank;
amtInTank -= pumped;
galSold = pumped + galSold;
count = pumped * price;
totalSales = count + totalSales;
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint)
<< setprecision(2);
cout << " Gallons requested from pump number " << position
<< ": " << pumpAmt << endl;
cout << " Gallons pumped: " << pumped << endl;
cout << " Gallons remaining in tank: " << amtInTank << endl;
cout << " The price of the sale is $" << (pumped * price) << endl;
cout << " Total gallons sold is: " << galSold << endl;
cout << " Total sales are $" << totalSales << endl;
return;
}
void Pump::newprice(float change)
{
price = change;
cout <<" The new price is $" << price << endl;
return;
}
void Pump::addgas(float gas)
{
amtInTank = amtInTank + gas;
if(amtInTank > tankCapacity)
amtInTank = tankCapacity;
cout << " Gas added, the new amount in the tank is: " << amtInTank << endl;
galSold = 0;
totalSales = 0;
return;
}
int main()
{
Pump First(1, 50, 1.50); // declare 2 object of type Pump
Pump Second(2, 100, 1.50);
First.current_status();
cout << endl;
First.request(20.0);
cout << endl;
First.newprice(1.60);
First.request(10.0);
First.addgas(50.0);
cout << endl;
First.current_status();
cout << endl << endl;
Second.current_status();
cout << endl;
Second.request(40.0);
cout << endl;
Second.newprice(1.60);
Second.request(20.0);
Second.addgas(100.0);
cout << endl;
Second.current_status();
cout << endl;
return 0;
}