write c++ programming that calculate electricity bill with persons name and id

Dani AI

Generated

asked for a C++ program to calculate an electricity bill with person name and id. Several replies reminded that an attempt should be shown () and to follow posting guidelines (). Below is a concise plan, a minimal, working C++ example (with placeholder tariff values), and practical tips to make the solution robust.

Plan: read customer ID (string), customer name (string, allow spaces), and units consumed (double). Validate that units >= 0. Compute the charge using slab rates (example slabs used below), add a fixed charge and a percentage tax, then format the total to two decimal places. Replace the example slab rates, fixed charge and tax with the local utility’s real values.

Example C++ (C++11) implementation:

#include <iostream>
#include <iomanip>
#include <string>

double calculateBill(double units) {
    double charge = 0.0;
    if (units <= 100) charge = units * 0.50;
    else if (units <= 200) charge = 100*0.50 + (units-100)*0.75;
    else if (units <= 500) charge = 100*0.50 + 100*0.75 + (units-200)*1.20;
    else charge = 100*0.50 + 100*0.75 + 300*1.20 + (units-500)*1.50;
    double fixedCharge = 30.0;   // placeholder
    double tax = 0.05 * charge;  // 5% example tax
    return charge + fixedCharge + tax;
}

int main() {
    std::string id, name;
    double units = 0.0;

    std::cout << "Customer ID: ";
    std::getline(std::cin, id);
    std::cout << "Customer Name: ";
    std::getline(std::cin, name);
    std::cout << "Units consumed: ";
    if (!(std::cin >> units) || units < 0) {
        std::cerr << "Invalid units value\n";
        return 1;
    }

    double total = calculateBill(units);
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "ID: " << id << "\nName: " << name << "\nUnits: " << units
              << "\nTotal Bill: $" << total << std::endl;
    return 0;
}

Notes and troubleshooting: test boundary cases (units = 100, 200, 500). Example: units=150 gives charge 87.50, tax 4.375, fixed 30 => total 121.88 after rounding. Use getline for names with spaces; if mixing >> then getline, consume the leftover newline. Store tariff slabs in a table or array for easier updates. As advised, post a specific attempt and any compiler errors if further help is needed.

Recommended Answers

All 5 Replies

OK. Done. Now what?

You accually have to try to do it yourself... if you have an issue or you dont understand somthing... than you post...

You cant seriously expect these awsome people here to just program things for you, thier here to help and give guidence.... not do you homework...

write program to print the kth digit from last e.g input 23617 and k=4 output 3 in c++

Mansoor.
It's incomprehensible how you could read the previous posts then add yours on the end.
Read them now.
Then read the thread that the previous post links to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.