Question #38100

Write a C++ program that calculates and displays water bill. The water rates vary depending on the type of usage.
 A code of ‘H’ means home use,
 A code of ‘C’ means commercial use,
 A code of ‘I’ means industrial use.
Any other code value should be treated as an INVALID INPUT. Water rates are computed as follows:
Code H:
First 1 hundred gallons for $5.00 and $0.005 for each additional gallon used
Code C:
First 4 hundred gallons for$1000.00 and $0.025 for each additional gallon used
Code I:
First 4 hundred gallons for $1500 and $0.125 for each additional gallon used
1

Expert's answer

2014-01-10T13:25:01-0500

Answer on Question #38100 – Programming - C++


#include <cstdlib>
#include <iostream>
using namespace std;
// main function
int main(int argc, char *argv[])
{
    char ch; // variable for type
    double gallons; // variable for volume of gallons
    double totalPrice = 0;
    // A code of 'H' means home use,
    cout << "H - home use\n";
    // A code of 'C' means commercial use,
    cout << "C - commercial use\n";
    // A code of 'I' means industrial use.
    cout << "I - industrial use\n";
    cout << "Select a type of using: ";
    cin >> ch;
    cout << "Enter total gallons amount: ";
    cin >> gallons; // read total volume of gallons
    switch(ch) {
        // Code H:
        // First 100 gallons for $5.00 and $0.005 for each additional gallon used
        case 'h':
        case 'H':
            if (gallons <= 100) {
                totalPrice = gallons * 5;
            } else {
                totalPrice = 100 * 5 + (gallons - 100) * 0.005;
            }
            break;
        // Code C:
        // First 400 gallons for $1000.00 and $0.025 for each additional gallon used
        case 'c':
        case 'C':
            if (gallons <= 400) {
                totalPrice = gallons * 1000;
            } else {
                totalPrice = 400 * 1000 + (gallons - 400) * 0.025;
            }
            break;
        // Code I:
        // First 400 gallons for $1500 and $0.125 for each additional gallon used
        case 'i':
        case 'I':
            if (gallons <= 400) {
                totalPrice = gallons * 1500;
            } else {
                totalPrice = 400 * 1500 + (gallons - 400) * 0.125;
            }
            break;
        default:
            cout << "\nInvalid input\n";
            break;
    }
    // show result
    cout << "Total water bill = $" << totalPrice << "\n";
    system("PAUSE"); // delay
    return EXIT_SUCCESS; // return 1
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS