Reputation: 31
so am trying to write a gas pump program. its compiling and working but my only problem is when i come to print out the final amounts of gallons and charge. when i press q and then enter to quit the loop and get the answer, its incrementing one more time. look at the displaycalc() function.
#include <iostream>
using namespace std;
class GasPump
{
private:
double gallons;
double charge;
double gasInTank ;
double costPerGallonReg;
double costPerGallonPlus;
double costPerGallonSup;
double costPerGallonChoice;
public:
void regularSetting();
void setPricePerGallon(double,double,double);
double getPricePerGallon();
void displayCalc();
};
//first function
void GasPump::regularSetting()
{
gallons = 0;
charge = 0;
gasInTank = 10.0;
cout<<"Price "<<charge<<endl;
cout<<"Gallons "<<gallons;
}
void GasPump::setPricePerGallon(double reg, double plus, double super)
{
cout<<"\n\nPlease choose type of gas you want to Purchase\n";
cout<<"\nRegular: "<<reg<<" Plus: "<<plus<<" Premium: "<<super;
//setting prices
costPerGallonReg = reg;
costPerGallonPlus = plus;
costPerGallonSup = super;
}
double GasPump::getPricePerGallon()
{
char choice;
cout <<"\nEnter r/R for Regular, p/P for Plus, or s/S for Super.\n";
cin >>choice;
if ((choice == 'R' || choice == 'r'))
{
return costPerGallonChoice = costPerGallonReg ;
}
else if ((choice == 'P'|| choice == 'p'))
{
return costPerGallonChoice = costPerGallonPlus ;
}
else if ((choice == 'S'|| choice == 's'))
{
return costPerGallonChoice = costPerGallonSup ;
}
else
{
cout<<"Invalid";
}
}
void GasPump::displayCalc()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
char choice;
system("cls");
cout<<"Press Enter to start filling and q to quit\n";
cout<<"Price "<<charge<<endl;
cout<<"Gallons "<<gallons<<endl;
while(gasInTank > 0)
{
choice = cin.get();
if((choice =='Q'||choice =='q'))
break;
system("cls");
cout<<"$"<<charge<<" ";
cout<<"Gallons "<<gallons<<endl;
charge += costPerGallonChoice * 0.1;
gallons += 0.1;
gasInTank -= 0.1;
}
cout<<"\nThank you for your purchase\n";
cout<<"Gallons: "<<gallons<<" $"<<charge;
}
int main()
{
//naming class
GasPump pump;
cout<<"Gas pump Program\n\n";
pump.regularSetting();
pump.setPricePerGallon(1.69,1.79,1.89);
pump.getPricePerGallon();
pump.displayCalc();
getchar();getchar();
return 0;
}
Upvotes: 0
Views: 139
Reputation: 56557
Your code is almost correct, there are 2 issues: flushing the cin
stream and moving the cout
in the while loop, code below:
cout<<"$"<<charge<<" ";
cout<<"Gallons "<<gallons<<endl;
at the end of the while loop, like
cin.ignore(1000, '\n');
cin.get();
cout<<"$"<<charge<<" ";
cout<<"Gallons "<<gallons<<endl; // if you really want to display the 0
while(gasInTank > 0)
{
choice = cin.get();
if((choice =='Q'||choice =='q'))
break;
system("cls");
// Move these 2 lines at the end
// cout<<"$"<<charge<<" ";
// cout<<"Gallons "<<gallons<<endl;
charge += costPerGallonChoice * 0.1;
gallons += 0.1;
gasInTank -= 0.1;
// moved here, now it's ok
cout<<"$"<<charge<<" ";
cout<<"Gallons "<<gallons<<endl;
}
Otherwise you are displaying before incrementing. Basically, when you pressed the first ENTER, you already put gas into the tank, the variables got incremented, but in your original code you were displaying the quantity before the increment.
Upvotes: 1