// This program does so and so ... // Author: .... Date: ... // ... // Metrowerks CodeWarrior version #include using namespace std; // declare global constants const int UNITS_PER_ROOM = 15; const int FIXED_UNIT_COST = 5; const int ADD_UNIT_COST = 2; double readDouble() // Safer way to read a double { double num; cin >> num; while(cin.fail()) // check for format errors { // in executing previous statement cin.clear(); // clear up the input stream cin.ignore(80, '\n'); // ignore remaining characters on the line cout << "Please enter the number correctly: "; cin >> num; } cin.ignore(80, '\n'); // ignore remaining characters on the line return num; } int readInt() // Safer way to read an int { int n; cin >> n; while(cin.fail()) // check for format errors { // in executing previous statement cin.clear(); // clear up the input stream cin.ignore(80, '\n'); // ignore remaining characters on the line cout << "Please enter the integer correctly: "; cin >> n; } cin.ignore(80, '\n'); // ignore remaining characters on the line return n; } char readChar() // safer way to read a single character { char c; cin >> c; cin.ignore(80, '\n'); // ignore remaining characters on the line return c; } int main() { // declare input variables int nosRooms; int oldMeterReading; int newMeterReading; char replyYorN; char quit; int totalCostPence; int totalCostPound; int remainderPence; bool cont = true; do { // Input Initial Data cout << "What is the number of rooms in your house? "; //cin >> nosRooms; cout << endl; nosRooms = readInt(); cout << "What is the OLD Meter reading? "; cin >> oldMeterReading; cout << endl; cout << "What is the NEW Meter reading? "; cin >> newMeterReading; cout << endl; // Perform Calculations 1 // Calculate Number of Units for Fixed Charge int fixedUnits = UNITS_PER_ROOM * nosRooms; // Calculate Fixed Charge in Pence int fixedCost = fixedUnits * FIXED_UNIT_COST; // Calculate Actual Number of Units int actualUnits = newMeterReading - oldMeterReading; // Is Actual Number than Fixed Charge Number? if (actualUnits > fixedUnits) { // TRUE Case // Calculate Additional Number of Units int aboveUnits = actualUnits - fixedUnits; // Calculate Total as Fixed Charge plus Additional Charge totalCostPence = fixedCost + aboveUnits * ADD_UNIT_COST; } else { // FALSE Case // Make Total equal to Fixed Charge totalCostPence = fixedCost; } // Is result in pounds and pence needed ? cout << "Do you want cost in pounds and pence (answer Y or N)? "; cin >> replyYorN; cout << endl; if (replyYorN == 'Y') { // Case Yes // Perform Calculations 2 // Find Number of Pounds in Total totalCostPound = totalCostPence / 100; // Find Remainder in Pence in Total remainderPence = totalCostPence % 100; // Output Total in Pounds and Pence cout << "Total Due: " << totalCostPound << " pounds and " << remainderPence << " pence." << endl; } else if (replyYorN == 'N') { // Case No // Output the Total Cost in pence cout << "Total Due: " << totalCostPence << " pence." << endl; } else { // Case neither Yes nor No - report Erroneous input cout << "Error in reply!" << endl; } cout << "Do you want to quit your calculation? (answer q, or press return) "; cin >> quit; cout << endl; if (quit == 'q') { cont = false; } } while (cont); cout << "the end of program" << endl; return 0; }