In: Computer Science
CODE:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double numOfGallons;
char gasType, isCarWashNeeded;
double
pricePerGallon=0.0,totalGasCost=0.0,costForCarWash=0.0,totalDue;
//constants to hold gasoline price
const double RegularGaslineCost = 2.89;
const double PlusGaslineCost = 3.09;
const double SuperGaslineCost = 3.39;
//read the number of gallons purchased
cout<<"Enter number gallons and press <Enter>: ";
cin>>numOfGallons;
//read the has type
cout<<endl<<"Enter gas type (R,P,S or N) and press
<Enter>: ";
cin>>gasType;
//read if car wash is needed or not
cout<<endl<<"Enter Y or N for car wash and press
<Enter>: ";
cin>>isCarWashNeeded;
//if gastype is R
if(gasType=='r' || gasType=='R')
{
//calulate total gas cost
totalGasCost=RegularGaslineCost * numOfGallons;
}
//if gastype is P
else if(gasType=='p' || gasType=='P')
{
//calulate total gas cost
totalGasCost=PlusGaslineCost * numOfGallons;
}
//if gastype is S
else if(gasType=='S' || gasType=='s')
{
//calulate total gas cost
totalGasCost=SuperGaslineCost * numOfGallons;
}
//if gastype is N
else if(gasType=='N' || gasType=='n')
{
//set price per gallon to 0
totalGasCost=0;
}
//else print error message and terminate program
else
{
cout<<"\nInvalid input. Try again..";
return 0;
}
//check if car wash is needed
if(isCarWashNeeded=='Y' || isCarWashNeeded=='y')
{
//if gaa cost is 10 or above
if(totalGasCost>=10)
{
//car wash is 1.25
costForCarWash=1.25;
}
else
{
//else car wash is 3.00
costForCarWash=3.00;
}
}
//if car wash is not needed set wash price to 0
else
{
costForCarWash=0.00;
}
//calculate total amount due
totalDue=totalGasCost+costForCarWash;
//print the ouput
cout<<"\n*****************************************\n";
cout<<"\n*\tGas-N-Cleas Service Station\t*\n";
cout<<"\n*\tMarch 2, 2004\t\t\t*\n";
cout<<"\n*****************************************\n";
cout<<"\nAmount of Gasoline
purchase\t"<<numOfGallons<<"
Gallons"<<endl;
if(gasType=='R' || gasType=='r')
cout<<"\nPrice per gallon \t\t"<<"
$"<<RegularGaslineCost<<endl;
if(gasType=='p' || gasType=='P')
cout<<"\nPrice per gallon \t\t"<<"
$"<<setprecision(2)<<PlusGaslineCost<<endl;
if(gasType=='s' || gasType=='S')
cout<<"\nPrice per gallon \t\t"<<"
$"<<setprecision(2)<<SuperGaslineCost<<endl;
if(gasType=='n' || gasType=='N')
cout<<"\nPrice per gallon \t\t"<<"
$"<<setprecision(2)<<pricePerGallon<<endl;
cout<<"\nTotal gasoline cost\t\t"<<"
$"<<fixed<<setprecision(2)<<totalGasCost<<endl;
cout<<"\nCar wash cost\t\t\t"<<"
$"<<fixed<<setprecision(2)<<costForCarWash<<endl;
cout<<"\nTotal due\t\t\t"<<"
$"<<fixed<<setprecision(2)<<totalDue<<endl;
cout<<endl<<"Thank you for stopping"<<endl;
cout<<endl<<"Please come again"<<endl;
cout<<endl<<"Remember to buckle up and drive
safely"<<endl;
return 0;
}
OUTPUT: