Calculate electric bill

Write down a program to calculate the fees charged on the house entered. The charge for 1 unit of electricity is RM 0.05. A value of 999 is entered to dismiss the loop. By using if else
Example output:
Number of house: 3 house
House 1 : 450 unit
House 2 : 400 unit
House 3 : 109 unit

Total Unit : 959 unit
Total Payment : RM 47.95

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <iomanip>

using namespace std;

float calculation (float);

int main()
{
	int count=0;
	float unit1=0, unit2=0, unit3=0; 
	float totalunit1=0, totalunit2=0, totalunit3=0;
	
	cout<<"House 1 : ";
	cin>>unit1;
	count++;
	
	if (unit1>0 && unit1<999)
	{
		totalunit1 = calculation(unit1);
		
		cout<<"House 2 : ";
		cin>>unit2;
		count++;
		
		if (unit2>0 && unit2<999)
		{
			totalunit2 = calculation(unit2);
		
			cout<<"House 3 : ";
			cin>>unit3;
			count++;
		
			if (unit3>0 && unit3<999)
			{
				totalunit3 = calculation(unit3);
				
				cout<<"\nNumber of house : "<<count<<" house";
				cout<<"\nHouse 1 : "<<unit1<<" unit";
				cout<<"\nHouse 2 : "<<unit2<<" unit";
				cout<<"\nHouse 3 : "<<unit3<<" unit"<<endl;
		
				cout<<"\nTotal Unit : "<<unit1+unit2+unit3<<" unit";
				cout<<"\nTotal Payment : RM "<<totalunit1+totalunit2+totalunit3;
			}
		
			else
			{
				totalunit3 = calculation(unit3);
		
				cout<<"\nNumber of house : "<<count<<" house";
				cout<<"\nHouse 1 : "<<unit1<<" unit";
				cout<<"\nHouse 2 : "<<unit2<<" unit";
				cout<<"\nHouse 3 : "<<unit3<<"unit"<<endl;
		
				cout<<"\nTotal Unit : "<<unit1+unit2+unit3<<" unit";
				cout<<"\nTotal Payment : RM "<<totalunit1+totalunit2+totalunit3;
			}
		}
		
		else
		{
			totalunit2 = calculation(unit2);
		
			cout<<"\nNumber of House : "<<count<<" house";
			cout<<"\nHouse 1 : "<<unit1<<" unit";
			cout<<"\nHouse 2 : "<<unit2<<" unit"<<endl;
		
			cout<<"\nTotal Unit : "<<unit1+unit2<<" unit";
			cout<<"\nTotal Payment : RM "<<totalunit1+totalunit2;
		}
		cout<<endl<<endl;
		system("pause");
	}
	
	else
	{
		totalunit1 = calculation(unit1);
		
		cout<<"\nNumber of house : "<<count<<" house";
		cout<<"\nHouse 1 : "<<unit1<<" unit"<<endl;
		
		cout<<"\nTotal Unit : "<<unit1<<" unit";
		cout<<"\nTotal Payment : RM "<<totalunit1;
	}
}

float calculation(float unitusage=0)
{
	float tusage=0;
	
	tusage = unitusage*0.05;
	
	return tusage;
}


Thank you for helping me
Last edited on
Please use the code format tags to format your code, it's unreadable as presented.

Also, I think you need to re-read the specification of what the program ought to do. What you've written isn't enough information to complete the algorithm.
I think what I do isn't correct. Can somebody tell me how can correct the algorithm.

I try and it lead me here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>

using namespace std;
class Electric
{
        float unit;
    public:
        void accept()
        {
                cout<<"\n Rumah : ";
                cin>>unit;
        }
};
int main()
{
	{
        Electric e[10];
        int i,house;
        cout<<"\n Number of house : ";
        cin>>house;
        for(i=0; i<house; i++)
                e[i].accept();
        return 0;


	}
}
Last edited on
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <limits>
#include <string>
#include <iomanip>

template<typename T = int>
auto getNum(const std::string& prm)
{
	const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
	T n {};

	while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return n;
}

int main()
{
	constexpr double charge {0.05};
	unsigned totunt {};

	for (unsigned unit {}, hno {1}; (unit = getNum<unsigned>("Enter whole units for house " + std::to_string(hno)+ " (999 to exit): ")) != 999; totunt += unit, ++hno);

	std::cout << "Total Units: " << totunt << '\n';
	std::cout << "Total payment: " << std::fixed << std::setprecision(2) << totunt * charge << '\n';
}

Hello Nik Hazreen,

Your first code is partly a good start, but you are repeating to much of the same code. And you are limited to only 3 houses. Not good.

You are either not understanding the instructions that you posted or there is more to what is needed. Also you are not following what you have posted.

It is best to post the full instructions that you were given and not what you think they mean.

Trying to follow the instructions I came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <iomanip>
#include <limits>  // <--- Added.
#include <string>  // <--- Added.

using namespace std;  // <--- Best not to use.

double calculation(double);

constexpr double RATE{ 0.05 };
constexpr int WIDTH{ 10 };
constexpr unsigned MAXSIZE{ 10 };

int main()
{
    const std::string PROMPT{ "\n Enter number of houses (-999 to quit): " };
    constexpr int END{ -999 };

    int count{};  // <--- ALWAYS initialize all your variables.
    int numOfHouses{};

    // <--- Kept for testing. Not really needed.
    // Can be reduced to 2 variables: "unitCost" and "totalUnitCost".
    double unit1{ 998.0 }, unit2{ 998.0 }, unit3{ 998.0 * 9 };
    double totalunit1{ unit1 * RATE }, totalunit2{ unit2 * RATE }, totalunit3{ unit3 * RATE };

    std::cout << std::fixed << std::setprecision(2);  // <--- Only needs done once.

    while (std::cout << PROMPT && !(std::cin >> numOfHouses) || numOfHouses < 1 || numOfHouses > MAXSIZE)
    {
        if (!std::cin)
        {
            std::cout << "\n     Invalid entry! Must be a number.\n";

            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
        }
        else if (numOfHouses == END)
        {
            break;
        }
        else if (numOfHouses < 1 || numOfHouses > MAXSIZE)
        {
            std::cout <<
                "\n"
                "     Invalid number of houses! Try again.\n"
                "     Must be at least 1 to a maximum of " << MAXSIZE << ".\n";
        }
    }

    do
    {
        if (numOfHouses == END)
        {
            continue;  // <--- Skips anything that follows and goes to the while condition.
        }

        std::cout << '\n';

        // TODO. Rest of code goes here.

        count++;  // <--- Moved.
    } while (count < numOfHouses && numOfHouses != END);

    if (numOfHouses != END)
    {
        cout <<
            " Number of house(s): " << numOfHouses << "\n"
            " House 1 :     " << std::setw(WIDTH) << unit1 << "\n"
            " House 2 :     " << std::setw(WIDTH) << unit2 << "\n"
            " House 3 :     " << std::setw(WIDTH) << unit3 << "\n"
            << std::string(17, ' ') << "========\n"
            " Total Units : " << std::setw(WIDTH) << unit1 + unit2 + unit3 << "\n"
            " Total Payment : RM" << totalunit1 + totalunit2 + totalunit3 << "\n";
    }

    return 0;
}

The first while loop will end when a number 1 -10 is entered. It also checks for a non numeric number being entered because a non numeric entry like a letter will cause "cin" to be put in a failed state and unusable the rest of the program until corrected or the program is ended.

The do/while loop will allow you to enter up to 10 houses. I based this on your 2nd code having an array of 10.

At this point it would be very easy to add the class and create an array of classes in "main". Not sure if a class is the best choice here. I would have used a struct for what little there is. Either one will work. It depends on what you have learned so far and what you need practice on.

For your class consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Electric
{
    double houseUsage{};
    double totalUnitCost{};

    public:
        void accept()
        {
            cout << "\n Penggunaan rumah: ";  // <--- Space before the (:) is not necessary. The space after is. Or your choice.
            cin >> houseUsage;

            totalUnitCost = calculation(houseUsage);
        }
};

This stores the usage for each house or each element of the array along with the total cost for that house.

Back in "main" under the TODO part you can use a for loop to enter the usage for each house and add to 2 variables for total usage and total cost.

The next for loop would be used for print out each house in the array along with the usage and cost.

When that for loop is finished you would print the totals.

I have not started on the TODO part yet not knowing what you want to do.

The "cout" at the end of the program is more for demonstration than actual use. It will need to be adjusted a bit.

See what you think.

Andy
ok thank you guys, your coding really help me with an idea. I have complete it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	cout<<"\n Welcome to Electric Bill Calculator :-)\n\n";
	cout<<"=== Program to calculate electric bill by entered total place ===\n\n";

	//variable declaration
	int n, i;
	float unit, totalpayment;
	int sum=0;

	//taking input from the command line (user)
	cout<<"\nMasukkan bilangan rumah = ";
	cin>>n;
	cout<<"\n\n";

	//taking n number as input from user and adding them to find the final sum
	for (i=0;i<n;i++)
	{
		cout<<"Rumah "<<i+1<<setw(19)<<"= "<<setw(1);
		cin>>unit;
			if (unit>=0 && unit < 999)
			{
				sum+=unit;
			}
			if (unit==999)
			{
				break;
			}
	}

	totalpayment=sum*0.05;

	cout<<"\n\nTotal Unit"<<setw(15)<<"= "<<sum<<endl;
	cout<<"Jumlah Bayaran"<<setw(11)<<"= "<<totalpayment<<endl<<endl;

	system("pause");

}

Topic archived. No new replies allowed.