int main() {
string name;
int amount;
float cost = 0;
float cost2 = 0;
float cost3 = 0;
// ask their name
cout << "Please enter yor name: ";
cin >> name; // Use getline() function
// ask the unit consumed
cout << "Enter units of electricity used: ";
cin >> amount;
// calulation to the bill
while (amount<0)
{
cout << "negative value, please enter units again. ";
cin >> amount;
}
// Instead of using while loops below, use if/else statements
while (amount <= 200)
{
cost = amount * 0.1 + 15;
cout << name << ", your electric bill is " << cost;
cin.get(); // Remove this
}
while (200 < amount < 500) // Change to (200 < amount && amount < 500)
{
cost2 = (200 * 0.1) + ((amount - 200) * 0.2) + 15;
cout << name << ", your electricity bill is " << cost2;
cin.get(); // Remove this
}
while (500 < amount)
{
cost3 = ((200 * 0.1) + ((amount - 200) * 0.2) + 15) + ((amount - 500) * 0.3);
cout << name << ", your electricity bill is " << cost3;
cin.get(); // Remove this
}// end calculation
}
Demonstrate your ability to use C++ syntax to develop code to calculate the electricity bill of the domestic users. The program uses a loop to prompt the user to enter the name of the user and units consumed and calculates the charges based on the following:
For the first 200 units – 10c per unit
For the next 300 units - 20c per unit
Beyond 500 units (that is 501 and above)– 30c per unit
All uses are charged a minimum of $15. If the total amount is more than $100 then an additional surcharge of 15% of the total amount is added.
The program prints the total charges for the user with the name. The loop is control via the sentinel value, ‘E’.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main(){
int units, totalUnits;;
bool E = true; // Sentinal value
double cost;
string name;
cout << "Enter your name: ";
getline(cin,name); // Reads the name including whitespace
while(E == true){ // Loop keeps running as long as the sential value stays true
cout << "Enter the number of units used: ";
cin >> units;
while(units < 0){ // Error check
cout << "You cannot have a negative number, please re-enter the number: ";
cin >> units;
}
totalUnits += units; // Keeps track of total unit
// The price range for the units
if(totalUnits <= 200){
cost += units * .1;
}
elseif(201 < totalUnits && totalUnits <= 500){
cost += units *.2;
}
else{
cost += units * .3;
}
// Asks if the user would like to input more information. 0 == false, 1 == true. 0 will terminate the loop
cout << "Would you like to contiue the program? Type '0' to end and '1' to enter more units to your bill.";
cin >> E;
}
// Minimum is $15, therefore the price will have to be at least 15
if(cost < 15){
cout << name << " you are being charged: $15.00\n";
}
// If the price is over 100, add 15% surcharge
elseif(cost > 100){
cost *= 1.15;
cout << "We will be adding a 15% surcharge to your bill.\n";
// Fixed and setprecision allows you to have 2 digits after decimal place
cout << name << " you are being charged: $" << fixed << setprecision(2) << cost << '\n';
}
else{
cout << name << " you are being charged: $" << fixed << setprecision(2) << cost << '\n';
}
return 0;
}