Answer on Question #46315, Programming, C++
Problem.
PGandE.cpp) Consider a gas and electric bill that sets two levels of use as follows:
Gas Electric
Baseline Quantities 31 therms 238.7 kwh
Baseline price .094 /kwh
Over Baseline Price .133 /kwh
Write a program that takes starting and ending readings for gas and electric meters and calculates charges to the nearest cent. Use one function to calculate charges for both gas and electric so as to eliminate duplicate calculations.
Solution.
Code
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Compute total price
float compute(float used, float quantity, float price, float overPrice) {
float total;
if (used < quantity) {
total = used * price;
} else {
total = quantity * price + (used - quantity) * overPrice;
}
return total;
}
int main() {
float sGas, sElectric;
float fGas, fElectric;
// Input/Output
cout << "Start gas: ";
cin >> sGas;
cout << "End gas: ";
cin >> fGas;
cout << "Gas price: $";
cout << fixed << setprecision(2) << compute(fGas - sGas, 31, 0.504, 0.824) << endl;
cout << "Start electric: ";
cin >> sElectric;
cout << "End electric: ";
cin >> fElectric;
cout << "Electric price: $";
cout << fixed << setprecision(2) << compute(fElectric - sElectric, 238.7, 0.094, 0.133) << endl;
return 0;
}Result
Start gas: 123
End gas: 170
Gas price: 4.32
http://www.AssignmentExpert.com/
Comments