#include "stdstuff.h" int computeConsumption (int initialReading, int finalReading) { if (finalReading < initialReading) { // correct the final reading finalReading += 10000; } return finalReading - initialReading; } double computeCost (int gasConsumed) { if (gasConsumed <= 70) { return 5.0; } if (gasConsumed <= 170) { return 5.0 + ((gasConsumed - 70) * 0.05); } if (gasConsumed <= 400) { return 5.0 + (100 * 0.05) + ((gasConsumed - 170) * 0.025); } // more than 400 cubic metres consumed return 5.0 + (100 * 0.05) + (230 * 0.025) + ((gasConsumed - 400) * 0.015); } void processReadings (int initialReading, int finalReading) { int gasConsumed; double amountDue; gasConsumed = computeConsumption (initialReading, finalReading); amountDue = computeCost (gasConsumed); cout << setiosflags (ios::fixed | ios::showpoint) << setprecision(2) << endl << "Cubic metres consumed: " << gasConsumed << " Amount Due: " << amountDue << endl; // no return required because this is a void function } int main () { int initialReading, finalReading; for (;;) { cout << endl << "Enter initial and final readings (-1 -1 to stop): "; cin >> initialReading >> finalReading; if ((initialReading < 0) || (finalReading < 0)) { break; } processReadings (initialReading, finalReading); } pause(); return 0; }