Project Review - Electricity Bill Calculating System

Hi Guys,
This is a Project on A topic which I chose.
I Kindly invite your suggestions and Positive feedback on this Project !





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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <fstream.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>

float dc[5]={3,3.25,3.5,4.6,6.6};
float df[3]={30,40,50};
float cf=120;


class BILL
{
	private:
		int sub_no;
		char sub_name[80];
		int unit;
		char category;
		float amount;
		void dcalc(int unit)
		{
			if((unit>0)&&(unit<=100))
				amount=df[0]+(unit*dc[0]);
			if((unit>0)&&(unit<=200))
				amount=df[0]+(unit*dc[1]);
			if((unit>200)&&(unit<=500))
			{
				amount=df[1]+(200*dc[2])+
				((unit-200)*dc[3]);
			}
			if(unit>500)
			{
				amount=df[2]+(200*dc[2])+
				(300*dc[3])+((unit-500)*dc[4]);
			}
		}
		void ccalc(int unit)
		{
			amount=cf+(unit*dc[3]);
		}
	public:
		int get_sub_no(){return sub_no;}
		char get_category(){return category;}
		void get_sub_det();
		void cdisp();
		void disp(char c);
};

void BILL::cdisp()
{
	cout<<sub_no<<"\t"<<sub_name;
	cout<<"\t\t\t"<<category;
	cout<<"\t\t"<<unit<<"\t"<<amount;
}

void BILL::disp(char c)
{
	cout<<"\nSubscriber Number         : "<<sub_no;
	cout<<"\nSubscriber Name           : ";
	puts(sub_name);
	cout<<"Category                  : "<<category;
	cout<<"\nUnits used (in Watt-Hour) : "<<unit<<"\n";
	if((c=='D')||(c=='d'))
	{
		float slab[5];
		if((unit>0)&&(unit<=100))
		{
			slab[0]=df[0];
			slab[1]=unit*dc[0];
			slab[2]=0;
			slab[3]=0;
		}
		if((unit>0)&&(unit<=200))
		{
			slab[0]=df[0];
			slab[1]=unit*dc[1];
			slab[2]=0;
			slab[3]=0;
		}
		if((unit>200)&&(unit<=500))
		{
			slab[0]=df[1];
			slab[1]=200*dc[2];
			slab[2]=(unit-200)*dc[3];
			slab[3]=0;
		}
		if(unit>500)
		{
			slab[0]=df[2];
			slab[1]=200*dc[2];
			slab[2]=300*dc[3];
			slab[3]=(unit-500)*dc[4];
		}
		cout<<"\nFixed Charges = Rs. "<<slab[0];
		int i=1;
		while(slab[i]!=0)
		{
			cout<<"\nSlab      ("<<i<<") = Rs. "<<slab[i];
			i++;
		}
	}
	else if((c=='C')||(c=='c'))
	{
		cout<<"\nFixed Charges = Rs. "<<cf;
		cout<<"\nBulk          = Rs. "<<(amount-cf);
	}
	cout<<"\n\nNet Amount : Rs. "<<amount;
	getch();
}

void BILL::get_sub_det()
{
	cout<<"\n\tData Entry :";
	cout<<"\n\t**** *****";
	cout<<"\n\nEnter Subscriber Number           : ";
	cin>>sub_no;
	cout<<"\nEnter Subscriber Name             : ";
	gets(sub_name);
	cout<<"\nEnter Category (Enter 'D' or 'C') : ";
	cin>>category;
	cout<<"\nEnter Units Consumed              : ";
	cin>>unit;
	if((category=='D')||(category=='d'))
	{
		dcalc(unit);
	}
	else if((category=='C')||(category=='c'))
		ccalc(unit);
	else
		cout<<"\nUnable to Process !!!";
}

void add_data()
{
	clrscr();
	BILL a;
	char ch='y',c;
	ofstream fout("CONSUMER.DAT",ios::binary|ios::app);
	while((ch=='y')||(ch=='Y'))
	{
		a.get_sub_det();
		fout.write((char*)&a,sizeof(a));
		cout<<"\nDo you want printed Bill ? (Y/N) :";
		cin>>c;
		if((c=='Y')||(c=='y'))
		{
			a.disp(a.get_category());
		}
		cout<<"\nProcess Another Bill ? (Y/N) :\n";
		cin>>ch;
	}
	fout.close();
}

void view_file()
{
	clrscr();
	BILL a;
	ifstream fin("CONSUMER.DAT",ios::binary);
	fin.seekg(0);
	cout<<"\nNo\tName\t\t\tCategory\tUnit\tAmount";
	cout<<"\n--\t----\t\t\t--------\t----\t------\n";
	while(fin.read((char*)&a,sizeof(a)))
	{
		a.cdisp();
		cout<<"\n";
	}
	fin.close();
}

void search()
{
	clrscr();
	BILL a;
	ifstream fin("CONSUMER.DAT",ios::binary);
	int s,c;
	cout<<"\nEnter Subscriber Number to Display :\n";
	cin>>s;
	fin.seekg(0);
	while(fin.read((char*)&a,sizeof(a)))
	{
		if(s==a.get_sub_no())
		{
			cout<<"\nDisplaying Details of Subscriber Number : "<<s;
			c=1;
			a.disp(a.get_category());
		}
	}
	if(c!=1)
		cout<<"\n\n\tNo Such Data Available to Display !!!\n\n";
	fin.close();
}

void remove()
{
	clrscr();
	BILL a;
	ifstream fin("CONSUMER.DAT",ios::binary);
	ofstream fout("temp.DAT",ios::binary);
	int n;
	char f='N';
	cout<<"\nEnter Subscriber Number to Delete :\n";
	cin>>n;
	fin.seekg(0);
	while(!fin.eof())
	{
		fin.read((char*)&a,sizeof(a));
		if(n==a.get_sub_no())
			f='Y';
		else
			fout.write((char*)&a,sizeof(a));
	}
	fin.close();fout.close();
	if(f=='Y')
		cout<<"\nDATA FOUND !\n";
	else
	{
		cout<<"\nDATA IS UNAVAILABLE !\n";
		f='U';
		goto abort;
	}
	char d;
	cout<<"Confirm Deletion ?(Y/N) :\t";
	cin>>d;
	if((d=='Y')||(d=='y'))
	{
		remove("CONSUMER.DAT");
		rename("temp.DAT","CONSUMER.DAT");
		cout<<"\nDeletion Successful !";
	}
	if((d=='N')||(d=='n'))
	{
		remove("temp.DAT");
		cout<<"\nDeletion Aborted by User !";
	}
	abort:
		if(f=='U')
			cout<<"\nABORTING DUE TO UNAVAILABLE DATA !\n";
}

void print()
{
	cout<<"\n\t\tPRESS 1 TO GO TO MAIN MENU";
	cout<<"\n\t\tPRESS 0 TO EXIT APPLICATION\n";
}

void main()
{
	clrscr();
	BILL a;
	int choice,ch;
	main:
		clrscr();
		cout<<"\n\tElectricity Bill Calculating System";
		cout<<"\n\t*********** **** *********** ******";
		cout<<"\n\n\t\t\tMAIN MENU :";
		cout<<"\n\t\t\t---- ----";
		cout<<"\n1.Add Consumer Data";
		cout<<"\n2.View All Consumers";
		cout<<"\n3.Search for a Particular Consumer";
		cout<<"\n4.Delete a Particular Consumer";
		cout<<"\n5.EXIT\n";
		cin>>ch;
		switch(ch)
		{
			case 1:
				add_data();
				print();
				cin>>choice;
				if(choice==1)
					goto main;
				else
					goto exit;
			case 2:
				view_file();
				print();
				cin>>choice;
				if(choice==1)
					goto main;
				else
					goto exit;
			case 3:
				search();
				print();
				cin>>choice;
				if(choice==1)
					goto main;
				else
					goto exit;
			case 4:
				remove();
				print();
				cin>>choice;
				if(choice==1)
					goto main;
				else
					goto exit;
			case 5:
				goto exit;
		}
	exit:
		cout<<"\n\nTHANKYOU FOR USING THIS APPLICATION !!!\n";
		for(int k=0;k<200;k++)
		{
			//Time Delay for-loop
		}
	getch();
}


//Project:Electricity_Bill_Calculator_Lines:310_Length:5937 




P.S : I know that this requires an older version of Neutron C++ . Thats because we are instructed to use this version only.
Last edited on
This is a Project on A topic which I chose.

And just what is this project supposed to be doing?


goto Really? Learn one of the other more accepted looping constructs.

The only comment in the program is line 304. Do you really expect someone to understand your program without meaningful comments?

As jlb said gotos are evil.

Lines 6-8: What are these (the names are meaningless) and why are they global?

Line 162,179,206,210: These statements only work because BILL is Plain Old Data (POD). If BILL were to contain any complex data types such as std::string, these statement would not work.

Line 246: main must be type int.

Line 249: Why is BILL declared here? It's not used in main.

Lines 267-272: Why are these statements repeated in each branch of the case since they apply to all branches. Move these statements after the switch statement.

Lines 302-305: A good optimizing compiler is going to remove these lines since they serve no purpose.


Line 251: A label with the same name as your function. Confusing and a poor practice.

Line 300: A label with the same name as a standard function exit(). Again confusing and a poor practice.

Last edited on
AbstractionAnon wrote:
Confusing and a poor practice.


but i am still a beginner only and i am not a programming genius like you all that is why i post my doubts so as to clear them as early as possible.
if you say it is a poor practise thak you for your generous critisism , i hope i can learn from you ,how to harass any one who is new to c++.

thank you !
jlb wrote:
And just what is this project supposed to be doing?


This is a Program to Calculate Energy consumption for a Household in kWh.
i hope i can learn from you ,how to harass any one who is new to c++.

The intent was never to harass you. Sorry if you felt harassed. My only intent was to guide you to better programming practices.
Topic archived. No new replies allowed.