Dear All,
I am reading a textbook "Absolute C++" by Walter Savitch. After a
chapter talking about classes, a programming project puzzled me:
Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
a. A display of the amount dispensed.
b. A display of the amount charged for the amount dispensed.
....
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.
I don't know how to let my object of class GasPump do f listed above. In
my class GasPump, there is a method start() and a method stop(). But:
GasPump myGasPump;
myGasPump.start(); //start pumping gas
//now, how can I stop pumping?
I am thinking using a boolean bHold. start() will set bHold = true and
stop() will set bHold = false. Inside start() method, once bHold is
true, the amount dispensed will keep increasing
void GasPump::start()
{
bHold = true;
double dAmount = 0.0;
while (bHold) //while handle is being pressed
{
dAmount += 0.1
} //now the handle has been released
//now the dAmount is the amount dispensed,
}
void GasPump::stop()
{
bHold = false;
}
But this seems getting into inter-processe communication(too complicated
to me) and I don't believe this is author's intention for this
programming project. I don't know how to deal with pressing a return
key, 0.1 gallons gas is added, either.
Could anybod kindly help me out? Thank you very much.