Question about possible errors in a program.

Greetings Everyone,

Once again I come to you with a completed homework assignment. However, even though this is completed, I would like to know the ways in which the program fails, and how to fix those errors. I do hope someone can help me. The program is designed to compute the charges per customer for AVISTA Utilities (which is a little weird since I use their services for my home). In any case, the program takes input from the user and spits out and answer. Thanks in advance for taking a look.

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
#include <iostream>
#include <cmath>
#include <cstring>
#include <iomanip>

using namespace std;

double residential()
{
	double energy; 
	double bcharge;
	double tres;
	bcharge = 4.0000;

	cout << "Please enter the energy usage\n";

	cin >> energy;

	if (energy <= 600)
	{
	   tres = bcharge + energy*(0.05255);
	   return tres;
	}
	else 
	{
	   tres = bcharge + energy*(0.06156);
	   return tres;
	}
} 
	   

double smcomm()
{
	double energy; 
	double demand;
	double tsmc;
	double bcharge = 6.0000;

	cout << "Please enter the energy usage\n";

	cin >> energy;

	cout << "Please enter the demand rate\n";

	cin >> demand;

	if (demand <= 20)
	{
	   tsmc = bcharge + energy + demand*(0.0000);
	   return tsmc;
	}
	else 
	{
	   tsmc = bcharge + energy + demand*(3.5000);
	   return tsmc;
	}
}

double lgcomm()
{
	double energy; 
	double demand;
	char discount;
	double tlgc;
	double bcharge = 6.0000;

	cout << "Please enter the energy usage:\n";

	cin >> energy;

	cout << "Please enter the demand rate:\n";

	cin >> demand;

	if (demand <= 20)
	{
	   tlgc = bcharge + energy*(0.07719) + demand*(0.0000);
	   cout << "Does this customer qualify for a discount? (Y/N):\n";
	   cin >> discount;
	   if (discount == 'Y')
	   {
		tlgc = tlgc*(0.2000);
	   }
	   else
	   {
		tlgc = tlgc;
	   }
	   return tlgc;
	}
	else
	{
	   bcharge + energy*(0.07719) + demand*(3.0000);
	   cout << "Does this customer qualify for a discount? (Y/N):\n";
	   cin >> discount;
	   if (discount == 'Y')
	   {
		tlgc = tlgc*(0.2000);
	   }
	   else
	   {
		tlgc = tlgc;
	   }
	   return tlgc;
	}
}

double indust()
{
	double energy;
	double demand;
	char discount;
	double tind;
	double bcharge = 0.0000;

	cout << "Please enter the energy usage:\n";

	cin >> energy;

	cout << "Please enter the demand rate:\n";

	cin >> demand;

	if (demand <= 50)
	{
	   tind = bcharge + energy*(0.05022) + 225.000;
	   cout << "Does this customer qualify for a discount? (Y/N):\n";
	   cin >> discount;
	   if (discount == 'Y')
	   {
		tind = tind + tind*(0.2000);
	   }
	   else
	   {
		tind = tind;
	   }
	   return tind;
	}
	else
	{
	   bcharge + energy*(0.05022) + demand*(2.75000);
	   return tind;
	}
}


int main()
{
	int ID, sched, Kwh, demand, discount, x;

	cout << endl;
	cout << "This program will give you\n"
	     << "the total energy charge for\n"
	     << "an AVISTA Utilities customer\n"
	     << "based on input from the user.\n";
	cout << endl;

	cout << "Please enter the customer ID number:\n";

	cin >> ID;

	if (ID == 000000)
	 {
	   cerr << "You entered 000000 for the customer ID number.\n"
	        << "This is not a valid customer.\n";
	 }

	cout << "Please enter the type of customer\n"
	     << "1 - Residential\n"
	     << "2 - Small Commercial\n"
	     << "3 - Large Commercial\n"
	     << "4 - Industrial\n";

	cin >> sched;

	double total;

	switch (sched)
	{
		case 1: cout << "You have entered a Residential customer type.\n";
			total = residential();
			cout << "Your total charge is: " << total << endl;
			break;

		case 2: cout << "You have entered a Small Commercial customer type.\n";
			total = smcomm();
			cout << "Your total charge is: " << total << endl;
			break;

		case 3: cout << "You have entered a Large Commercial customer type.\n";
			total = lgcomm();
			cout << "Your total charge is: " << total << endl;
			break;

		case 4: cout << "You have entered an Industrial customer type.\n";
			total = indust();
			cout << "Your total charge is: " << total << endl;
			break;

		default: cout << "You have not entered a valid customer type.\n";
	}
	return 0;
}
I tried it briefly and it worked if you add
1
2
cin.ignore().get();
   


just before the return 0; }

ie;
1
2
3
cin.ignore().get();
    return 0;
}


apart from that it COMPILED without any problems.

But, I think the code lines like bcharge + energy*(0.05022) + demand*(2.75000);
are going to give you problems and need breaking down.

Last edited on
Thanks!
But, I think the code lines like bcharge + energy*(0.05022) + demand*(2.75000);
are going to give you problems and need breaking down.

I don't see why. The unnecessary brackets are an eyesore, though.
The following code shows two different results using the same variables.
If the code is contiguous and not broken up a completely different result occurs.
Unless this is what Flyn380 wants, I dont know, but it is something Ive always had problems with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{

        double bcharge = 2;
	double energy = 2;
	double demand = 2;

	double in = bcharge + energy;  //=4
	double rn = in * 0.5;  //=2
	double tn = rn + demand;  //=4
	double xn = tn * 2;  //=8   (correct result!)
	cout << xn<<endl;   // =8   (correct result!)

//when the code is contiguous a different result occurs.
        double otherresult = bcharge + energy * 0.5 + demand * 2;   // the result is 7 ?????
	cout<< otherresult <<endl; // =7  ???


	cin.ignore().get();
	return 0;
}
Last edited on
The other way to break down the line of code is using parenthesis.

1
2
    double other = ( ( ( ( bcharge + energy ) * 0.5 )   + demand )   * 2 );      // =8  (correct result)
    cout<< other <<endl;     // =8  (correct result) 
It seems you're ignoring mathematical precedence rules. Saying

bcharge + energy * 0.5 + demand * 2

is equivalent to saying

bcharge + (energy * 0.5) + (demand * 2)

because multiplication has higher precedence than addition.
closed account (D80DSL3A)
reply ignored. Content deleted.
Last edited on
I will try and change the code to something more like what has been suggested. I found out today that I am not supposed to be prompting the user for the entries for each of the variables listed. Instead a data file like "data.txt" will be used on the program in order to produce results. I spent a bunch of time on the code today in the lab and found multiple errors in the math (where I totally missed the mathematical prededence.. duh) as well as an endless loop from one of the functions. The other thing that I need to do is verify the input values given. I asked about using the cin.fail() and was told that while that would work it won't work in every case. The problem is, I have no idea how to do that. I will post the code here shortly for folks to look and and possibly give me a shove in the right direction. For now though I have a little bit more clean up to do before I post the code. Thanks again for everyone's help.
So the code as it stands now is as follows:

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
#include <iostream>
#include <cmath>
#include <cstring>
#include <iomanip>

using namespace std;

double residential()
{
	double energy; 
	double bcharge;
	double tres; // Defining the total for a Residential
	bcharge = 4.0000;

	cin >> energy;

	if (energy <= 600)
	{
	   tres = bcharge + (energy * 0.05255); 
	   // 0.05255 is from lab info
	   return tres;
	}
	else 
	{
	   tres = bcharge + (600 * 0.05255) + ((energy-600) * 0.06156); 
	   // The value 0.06156 is from lab info
	   // The value 600 is the first 600Kw of energy used
	   return tres;
	}
} 
	   

double smcomm()
{
	double energy; 
	double demand;
	double tsmc; // Defining the total for a Small Commercial
	double bcharge = 6.0000;

	cin >> energy;

	cin >> demand;

	if (demand <= 20)
	{
	   tsmc = bcharge + (energy * 0.07971);
	   // The value 0.07971 is from the lab info
	   return tsmc;
	}
	else 
	{
	   tsmc = bcharge + (energy * 0.07971) + ((demand-20) * 3.5000);
	   // The values 0.07971 and 3.5000 are from lab info
	   return tsmc;
	}
}

double lgcomm()
{
	double energy; 
	double demand;
	char discount;
	double tlgc; // Defining the total for a Large Commercial
	double bcharge = 6.0000;

	cin >> energy;

	cin >> demand;

	if (demand <= 20)
	{
	   tlgc = bcharge + (energy * 0.07719);
	   
	   cin >> discount;
	   
	   if (discount == 'Y')
	   {
		tlgc = tlgc;
	   }
	   else
	   {
		tlgc = tlgc;
	   }
	   return tlgc;
	}
	else
	{
	   bcharge + (energy * 0.07719) + ((demand-20) * 3.0000); 
	   // The numbers 0.07719 and 3.0000 above are from the lab info

	   cin >> discount;

	   if (discount == 'Y')
	   {
		tlgc = tlgc - (tlgc * 0.80000);  
		// The 0.8000 is from 1.00000 - 0.20000
	   }
	   else
	   {
		tlgc = tlgc;
	   }
	   return tlgc;
	}
}

double indust()
{
	double energy;
	double demand;
	char discount;
	double tind;

	cin >> energy;

	cin >> demand;

	if (demand <= 50)
	{
	   tind = (energy * 0.05022) + 225.000; 
	   // The number 0.05022 and 225.000 used here are from the lab info

           cin >> discount;

	   if (discount == 'Y')
	   {
	      tind = (energy * 0.05022) + (225.000 * 0.80000);
	      // 0.80000 is from 1.000 - 0.20000
	   }
	   else
	   {
	      tind = tind;	
	   }
	}
	else
	{
	   tind = (energy * 0.05022) + 225.000 + ((demand-50)*(2.75000));
	

           cin >> discount;

	   if (discount == 'Y')
	   {
	      tind = (energy*0.05022)+(225.000*0.80000)+(((demand-50)*(2.75000))*0.80000);
	      // demand-50 is because only the demand above 50 is multiplied
	   }
	   else
	   {
	      tind = tind;
	   }
	}
        return tind;

}


int main()
{
	int ID, sched;

	cout << endl;
	cout << "This program will give you\n"
	     << "the total energy charge for\n"
	     << "an AVISTA Utilities customer\n"
	     << "based on input.\n";
	cout << endl;

	cin >> ID;

	while(ID != 000000)
	{

		if (ID == 000000)
		{
		   cerr << "This is not a valid customer.\n";
		}

		cin >> sched;

		double total;

		switch (sched)
		{
			case 1:  total = residential();
				 cout << ID << "\t " << total << endl;
				 break;
	
			case 11: total = smcomm();
				 cout << ID << "\t " << total << endl;
				 break;
	
			case 12: total = lgcomm();
				 cout << ID << "\t " << total << endl;
				 break;
	
			case 21: total = indust();
				 cout << ID << "\t " << total << endl;
				 break;
	
			default: cout << "You have not entered a valid customer type.\n";
		}
		cin >> ID;
	}
	return 0;
}


Also we were given several input text files and I have run the program with those files and the correct output is obtained. For example:

input1.txt:
178531 1 540
459875 11 851 15
629875 1 1024
294653 12 11441 22 N
258735 21 20450 31 N
689237 11 1551 28
375960 1 300
486574 21 25142 70 Y
834765 12 9142 18 Y
198763 21 5413 55 N
335467 1 1873
154674 21 4795 44 Y
000000


178531	32.38
459875	73.83
629875	61.63
294653	895.13
258735	1252.00
689237	157.63
375960	19.77
486574	1486.63
834765	711.67
198763	510.59
335467	113.90
154674	420.80


Also, I know there is probably a lot of extra stuff that is an eyesore for more experienced coders. I hope you can bear with me as I learn this stuff. I am sure that the code will become much cleaner as I progress.
Topic archived. No new replies allowed.