Loop switch statement

How to loop this switch statement? Please check my code
The program will calculate the bill amount for each customer and prints the bill. The process will continue until the user requests to stop.

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

using namespace std;

int main (void)
{
string name, address;
char category, nextCount;
int unitsC, unitsD;
double billC, billD;
int c=0, d=0;

cout << endl<< "Please enter your name: " ;
getline (cin, name);
cout <<  "Please enter your address: ";
getline (cin,address);
cout <<  "Please enter your category, (Domestic: D, Commercial: C): ";
cin >> category;

switch (category)
{case 'D': cout << "Please enter units used: ";
cin >> unitsD;
if (unitsD <= 200) {billD = 0.1 * unitsD;}
else if (unitsD > 200 && unitsD <= 300 ) {billD =( (0.1 * 200) + (0.12 * (unitsD-200)));}
else if (unitsD > 300) {billD= ((0.1 * 200) + (0.12 * 100) + (0.09 * (unitsD -300) ));}
cout << "Your bill is RM " << billD<< endl;
cout << "Next customer? (Y: Yes, S: Stop): ";
cin >> nextCount;
d++;
break;
case 'C':cout << "Please enter units used: ";
cin >> unitsC;
if (unitsC <= 200) {billC = 0.2 * unitsC;}
else if (unitsC > 200 && unitsC <= 300 ) {billC =( (0.2 * 200) + (0.22 * (unitsC-200)));}
else if (unitsC > 300) {billC= ((0.2 * 200) + (0.22 * 100) + (0.18 * (unitsC-300) ));}
cout << "Your bill is RM " << billC<< endl;
cout << "Next customer? (Y: Yes, S: Stop): ";
cin >> nextCount;
c++;

}
}
So did you learn anything from your previous thread on the same subject?
https://cplusplus.com/forum/beginner/284282/
Perhaps something like:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

int main(void) {
	unsigned c {}, d {};
	unsigned char nextCount {};

	do {
		std::string name, address;
		unsigned char category;
		double bill { };
		bool bad {};

		std::cout << "\nPlease enter your name: ";
		std::getline(std::cin, name);

		std::cout << "Please enter your address: ";
		std::getline(std::cin, address);

		std::cout << "Please enter your category, (Domestic: D, Commercial: C): ";
		std::cin >> category;

		switch (static_cast<char>(std::toupper(category))) {
			case 'D':
				{
					int unitsD {};

					std::cout << "Please enter units used: ";
					std::cin >> unitsD;

					if (unitsD <= 200)
						bill = 0.1 * unitsD;
					else if (unitsD > 200 && unitsD <= 300)
						bill = ((0.1 * 200) + (0.12 * (unitsD - 200)));
					else if (unitsD > 300)
						bill = ((0.1 * 200) + (0.12 * 100) + (0.09 * (unitsD - 300)));
				}
				++d;
				break;

			case 'C':
				{
					int unitsC {};

					std::cout << "Please enter units used: ";
					std::cin >> unitsC;

					if (unitsC <= 200)
						bill = 0.2 * unitsC;
					else if (unitsC > 200 && unitsC <= 300)
						bill = ((0.2 * 200) + (0.22 * (unitsC - 200)));
					else if (unitsC > 300)
						bill = ((0.2 * 200) + (0.22 * 100) + (0.18 * (unitsC - 300)));
				}
				++c;
				break;

			default:
				bad = true;
				std::cout << "Invalid option\n";
				break;
		}

		if (!bad)
			std::cout << "Your bill is RM " << bill << '\n';

		std::cout << "Next customer? (Y: Yes, S: Stop): ";
		std::cin >> nextCount;
		std::cin.ignore(1000, '\n');
	} while (static_cast<char>(std::toupper(nextCount)) == 'Y');

	std::cout << "C was used " << c << '\n';
	std::cout << "D was used " << d << '\n';
}

some totally unrelated tips...

variables are your friend. Magic numbers (eg, 0.2, 0.18) are difficult to follow later when you try to edit and fix a bug in the code 3 years after you (or worse, someone else) wrote it.

inverted logic makes the conditionals easier to write and follow. that is, >300 else if > 200 else
is easier than the way you wrote it. Its probably possible to avoid the conditions entirely, but lets not do that right now.

the case blocks are virtually identical apart from some magic numbers.
slightly easier to read and follow would be something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case 'D':
units = unitsD;
pct1 = 0.1; //better, these could be in a container, even a C array, but baby steps...
pct2 = 0.12 //explain what each on is with a comment, eg "this is the percent of furlongs per fortnight when traveling across antartica"
pct3 = 0.09
break;
case 'C'
units = unitsC;
pct1 = 0.2;
pct2 = 0.22
pct3 = 0.18

if( units > 300)
bill = ((pct1 * 200) + (pct2 * 100) + (pct3 * (units - 300)));
else
... etc


what this does is factor out the magic constants and only do the computations in one place.
this is useful because its the same computations for each type. If they were different, eg if C type were instead < 500 else < 1000 else < 1500, you would need to do it the way you have it now or make those values variables as well and manage that too.

all that combined greatly reduces the redundancy and size of your code, making it easier to read.
Last edited on
Topic archived. No new replies allowed.