Counting loops in switch statement.

Hi, I need to count how many times A/B/C/D has been executed in this loop-switch statement. Here is my code:

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
  #include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main (void)
{
	int radiusA, lengthB, lengthC, widthC, heightD, baseD, i ;
	double pi= 3.142;	
	char Area;
	
	
		cout << "      CALCULATION OF AREA\n\n";
		cout << "      CHOICE       SHAPE\n";
		cout <<  setw(10) << "A" << "     " << setw(10) << "Circle" << endl;
		cout <<  setw(10) << "B" << "     " << setw(10) << "Square" << endl;
		cout <<  setw(10) << "C" << "     " << setw(13) << "Rectangle" << endl;
		cout <<  setw(10) << "D" << "     " << setw(12) << "Triangle" << endl;
		cout <<  setw(10) << "E" << "     " << setw(8) << "Exit" << endl << endl;
		
		
	 while ( Area != 'E'  )
	 {
	cout << endl << "      Please enter your choice: ";
	cin >> Area;
	{
	 switch (Area){
	 
	 case 'A' : cout << "   Insert the radius: ";
	            cin >> radiusA;
	            cout << "   The area of the circle is " << pi * radiusA << endl;
	 break;
	 case 'B' : cout << "   Insert the length: ";
	            cin >> lengthB;
	            cout << "   The area of the square is " << pow (lengthB,2) << endl;
	 break;
	 case 'C' : cout << "   Insert the length: ";
	            cin >> lengthC;
	            cout << "   Insert the width: ";
	            cin >> widthC;
	            cout << "   The area of the rectangle is " << lengthC * widthC << endl;
	 break;
	 case 'D' : cout << "   Insert the height: ";
	            cin >> heightD;
	            cout << "   Insert the base: ";
	            cin >> baseD;
	            cout << "   The area of triangle is: " << (baseD * heightD) / 2 << endl;
	 break;
	 case 'E' : cout << "   You exit the program. " << endl;
	 break;
	 default: cout << "   Invalid Input" << endl ;}
}
}
 
 	cout << "   Program exited. " << endl;
 		
}.
Last edited on
You will obviously need a separate counter for each case and increment them within the switch.
Can I get code example for one case? I still dont get it.
Perhaps something like (which also corrects other issues in the code - revise your maths for area of a circle...):

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
#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>

using std::cin;
using std::cout;
using std::setw;

int main() {
	unsigned cntA {}, cntB {}, cntC {}, cntD {};

	cout << "      CALCULATION OF AREA\n\n";
	cout << "      CHOICE       SHAPE\n";
	cout << setw(10) << "A" << "     " << setw(10) << "Circle\n";
	cout << setw(10) << "B" << "     " << setw(10) << "Square\n";
	cout << setw(10) << "C" << "     " << setw(13) << "Rectangle\n";
	cout << setw(10) << "D" << "     " << setw(12) << "Triangle\n";
	cout << setw(10) << "E" << "     " << setw(8) << "Exit\n\n";

	for (char Area {}; Area != 'E';) {
		cout << "\n      Please enter your choice: ";
		cin >> Area;

		switch (Area = static_cast<char>(std::toupper(static_cast<unsigned char>(Area)))) {
			case 'A':
				{
					double radiusA {};

					cout << "   Insert the radius: ";
					cin >> radiusA;
					cout << "   The area of the circle is " << M_PI * radiusA * radiusA << '\n';
				}
				++cntA;
				break;

			case 'B':
				{
					double lengthB {};

					cout << "   Insert the length: ";
					cin >> lengthB;
					cout << "   The area of the square is " << lengthB * lengthB << '\n';
				}
				++cntB;
				break;

			case 'C':
				{
					double lengthC {}, widthC {};

					cout << "   Insert the length: ";
					cin >> lengthC;
					cout << "   Insert the width: ";
					cin >> widthC;
					cout << "   The area of the rectangle is " << lengthC * widthC << '\n';
				}
				++cntC;
				break;

			case 'D':
				{
					double heightD {}, baseD {};

					cout << "   Insert the height: ";
					cin >> heightD;
					cout << "   Insert the base: ";
					cin >> baseD;
					cout << "   The area of triangle is: " << baseD * heightD / 2.0 << '\n';
				}
				++cntD;
				break;

			case 'E':
				break;

			default:
				cout << "   Invalid Input\n";
				break;
		}
	}

	cout << "A was executed " << cntA << '\n';
	cout << "B was executed " << cntB << '\n';
	cout << "C was executed " << cntC << '\n';
	cout << "D was executed " << cntD << '\n';
	cout << "   Program exited.\n";
}

work smarter:

unsigned int counter[256]{}; //overkill, this can count any ascii input without a lot of thinking about it.
..blah blah
cin >> Area;
counter[Area]++;
... blah blah switch and such
...
cout << "A was executed " << counter['A']; //etc format text as you see fit.
Last edited on
Interesting little program for beginners.
Maybe I misunderstood your question, but why don't you use some simple incrementations with integers? In each case section, you can ++ them, displaying them at the end of the process. This is so easy. For this reason, I guess that I misunderstood something. Let me know...
@Geckoo - that is how I understood the question, just some simple integer increments as per my code above. Jonnin idea's more general (and better IMO) but I didn't know if they had covered arrays....

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
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
#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>

using std::cin;
using std::cout;
using std::setw;

int main() {
	unsigned cnts[256] {};

	cout << "      CALCULATION OF AREA\n\n";
	cout << "      CHOICE       SHAPE\n";
	cout << setw(10) << "A" << "     " << setw(10) << "Circle\n";
	cout << setw(10) << "B" << "     " << setw(10) << "Square\n";
	cout << setw(10) << "C" << "     " << setw(13) << "Rectangle\n";
	cout << setw(10) << "D" << "     " << setw(12) << "Triangle\n";
	cout << setw(10) << "E" << "     " << setw(8) << "Exit\n\n";

	for (unsigned char Area {}; Area != 'E';) {
		cout << "\n      Please enter your choice: ";
		cin >> Area;

		++cnts[Area = static_cast<unsigned char>(std::toupper(Area))];

		switch (Area) {
			case 'A':
				{
					double radiusA {};

					cout << "   Insert the radius: ";
					cin >> radiusA;
					cout << "   The area of the circle is " << M_PI * radiusA * radiusA << '\n';
				}
				break;

			case 'B':
				{
					double lengthB {};

					cout << "   Insert the length: ";
					cin >> lengthB;
					cout << "   The area of the square is " << lengthB * lengthB << '\n';
				}
				break;

			case 'C':
				{
					double lengthC {}, widthC {};

					cout << "   Insert the length: ";
					cin >> lengthC;
					cout << "   Insert the width: ";
					cin >> widthC;
					cout << "   The area of the rectangle is " << lengthC * widthC << '\n';
				}
				break;

			case 'D':
				{
					double heightD {}, baseD {};

					cout << "   Insert the height: ";
					cin >> heightD;
					cout << "   Insert the base: ";
					cin >> baseD;
					cout << "   The area of triangle is: " << baseD * heightD / 2.0 << '\n';
				}
				break;

			case 'E':
				break;

			default:
				cout << "   Invalid Input\n";
				break;
		}
	}

	for (char ch : {'A', 'B', 'C', 'D'})
		cout << ch << " was executed " << cnts[ch] << " times\n";

	cout << "   Program exited.\n";
}

Last edited on
Its really the same idea, just a more compact implementation by putting the counters in an array and lazily associating them to the possible inputs thx to the beauty of ascii (its a wonderful code, long as you speak english!).
at the end of the day you still have 5 or whatever counters active getting bumped based off the input.
Last edited on

Thanks I have got my answer:

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
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main (void)
{
	int radiusA, lengthB, lengthC, widthC, heightD, baseD, i ;
	double pi= 3.142;	
	char Area;
 int a=0, b=0, c=0, d=0;
	
	
		cout << "      CALCULATION OF AREA\n\n";
		cout << "      CHOICE       SHAPE\n";
		cout <<  setw(10) << "A" << "     " << setw(10) << "Circle" << endl;
		cout <<  setw(10) << "B" << "     " << setw(10) << "Square" << endl;
		cout <<  setw(10) << "C" << "     " << setw(13) << "Rectangle" << endl;
		cout <<  setw(10) << "D" << "     " << setw(12) << "Triangle" << endl;
		cout <<  setw(10) << "E" << "     " << setw(8) << "Exit" << endl << endl;
		
		
	 while ( Area != 'E'  )
	 {
	cout << endl << "      Please enter your choice: ";
	cin >> Area;
	{
	 switch (Area){
	 
	 case 'A' : cout << "   Insert the radius: ";
	 cin >> radiusA;
	 cout << "   The area of the circle is " << pi * radiusA << endl;
	 a++;
  break;
	 case 'B' : cout << "   Insert the length: ";
	 cin >> lengthB;
	 cout << "   The area of the square is " << pow (lengthB,2) << endl;
	 b++;
  break;
	 case 'C' : cout << "   Insert the length: ";
	 cin >> lengthC;
	 cout << "   Insert the width: ";
	 cin >> widthC;
	 cout << "   The area of the rectangle is " << lengthC * widthC << endl;
	 c++;
  break;
	 case 'D' : cout << "   Insert the height: ";
	 cin >> heightD;
	 cout << "   Insert the base: ";
	 cin >> baseD;
	 cout << "   The area of triangle is: " << (baseD * heightD) / 2 << endl;
	 d++;
  break;
	 case 'E' : cout << "   You exit the program. " << endl;
	 break;
	 default: cout << "   Invalid Input" << endl ;}
}
}
  cout << " A= "<< a << " B= " << b << " C= " << c << " D= " << d << endl; 
 	cout << "   Program exited. " << endl;
 		
}
yes - but you should look at and digest the code provided. In particular your line 33...
@Andy99,

Your formula to calculate the area of a circle is wrong when using the radius. You are using the formula when the diameter is known.

A circle's diameter is double the radius.

The correct formula when the radius is known is shown by seeplus.

And IMO using the C library pow function to calculate the area of a square is a bit of performance overkill. Not wrong, just rather hackish, since an int is promoted to a double by casting.

https://en.cppreference.com/w/cpp/numeric/math/pow
(see the note for overload 7)

Use 2 times the entered length (2 * lengthB). Or length times length (lengthB * lengthB).

For that matter your inputs should be doubles instead of ints. Using ints you run the risk of "integer math" issues with the results being truncated.
also use <cmath> not math.h. Math.h is similar, but its the C header file and is missing a few bits that you will need for other projects.
You are using the formula when the diameter is known.


pi * diam gives the length of the circumference...

pi * radius gives half the circumference length
Oh okay I overlooked it. Thanks
Topic archived. No new replies allowed.