In: Computer Science
A gas station wants a program to keep track of sales. Your gas station sells diesel for 107.9 cents per litre and regular gas for 112.9 cents per litre. Have the user enter the type of fuel (1 = Regular gas, 2 = Diesel) and number of litres sold. Print out a total for each sale (remember fuel prices already include the GST). Once you enter a 0 for the type of fuel your program should stop and print out a table summarizing the totals for each fuel type as shown in the print out below
The following is a sample run, user input is shown as bold underline:
Enter type of sale (1=Regular, 2=Diesel, 0=exit): 1
How many litres? 40.2
Regular Gas
Litres Sold = 40.2
Sale total $45.39
Enter type of sale (1=Regular, 2=Diesel, 0=exit): 2
How many litres? 100
Diesel
Litres Sold = 100.0
Sale total $107.90
Enter type of sale (1=Regular, 2=Diesel, 0=exit): 2
How many litres? 5
Diesel
Litres Sold = 5.0
Sale total $5.40
Enter type of sale (1=Regular, 2=Diesel, 0=exit): 1
How many litres? 10
Regular Gas
Litres Sold = 10.0
Sale total $11.29
Enter type of sale (1=Regular, 2=Diesel, 0=exit): 0
Station Totals
Type Litres Total
Regular 50.2 56.68
Diesel 105.0 113.30
Totals 155.2 169.98
C++ code for the given program specification is provided below. File name: gas_station.cpp
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
float diesel_per_lt = 107.9, gas_per_lt = 112.9, lt_fuel, Total, Reg_lt = 0, diesel_lt = 0, Reg_total = 0, diesel_total = 0;
int input = 3, i=0;
float sales[100][3];
while(input != 0) {
cout << "\nEnter type of sale (1=Regular, 2=Diesel, 0=Exit): ";
cin >> input;
if(input == 0)
break;
if(input == 1) {
cout << "How many litres? ";
cin >> lt_fuel;
Reg_lt += lt_fuel;
sales[i][0] = input;
sales[i][1] = lt_fuel;
sales[i][2] = lt_fuel * gas_per_lt / 100;
Reg_total += sales[i][2];
cout << "Regular Gas\nLitres Sold = " << lt_fuel << "\nSale Total = " << fixed << setprecision(2) << sales[i][2];
i++;
}
else if(input == 2) {
cout << "How many litres? ";
cin >> lt_fuel;
diesel_lt += lt_fuel;
sales[i][0] = input;
sales[i][1] = lt_fuel;
sales[i][2] = lt_fuel * diesel_per_lt / 100;
diesel_total += sales[i][2];
cout << "Diesel\nLitres Sold = " << lt_fuel << "\nSale Total = " << sales[i][2];
i++;
}
}
cout << "\nStation Totals\n";
cout << setw(15) << "Type" << setw(15) << "Litres" << setw(15) << "Total" << endl;
cout << setw(15) << "Regular" << setw(15) << fixed << setprecision(1) << Reg_lt << setw(15) << fixed << setprecision(2) << Reg_total << endl;
cout << setw(15) << "Diesel" << setw(15) << fixed << setprecision(1) << diesel_lt << setw(15) << fixed << setprecision(2) << diesel_total << endl;
cout << setw(15) << "Totals" << setw(15) << fixed << setprecision(1) << Reg_lt + diesel_lt << setw(15) << fixed << setprecision(2) << Reg_total + diesel_total << endl;
return 0;
}
Instructions to execute the code:
1. Move to the directory where source file is saved in your command line.
2. Compile the code using the following command:
> g++ gas_station.cpp
3. Run the code using the following command:
> ./a.out
-------------------------------------------------------------------------------------------------------------------------------------