Using switch statement to get same output

How would you write out the code using switch statements to get the same output. If you can, please write a comment next to the lines changed to provide a brief explanation of what that line does. I am a beginner and looking to learn more about this language.
Thanks in advance!

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
#include<iostream>
using namespace std;
int main()
{int choice;
 double s, l, w , r, d, b, h, ls, pi = 3.14159;
 do{cout<<"\nGeometry Calculator"
 		<<"\n-------------------"
 		<<"\n1) SQUARES"
 		<<"\n2) RECTANGLES"
 		<<"\n3) CIRCLES"
 		<<"\n4) TRIANGES"
 		<<"\n5) Quit"
 		<<"\nYour Selection? ";
 	cin>>choice;
 	if(choice==1)
 		{cout<<"\nSide Legnth? ";
 		 cin >> s;
 		 cout<<"\nPerimeter is ="<<s*4
 		 	 <<"\nArea is = "<<s*s<<"\n";
 		}
 	if(choice==2)
 		{cout<<"\nLength? ";
 		 cin>>l;
 		 cout<<"\nWidth? ";
 		 cin>>w;
 		 cout<<"\nPerimeter is = "<<l+l+w+w
 		 	 <<"\nArea is = "<<l*w;
		}
	if(choice==3)
		{cout<<"\nRadius? ";
		 cin>>r;
		 cout<<"\nDiameter = "<<2*r;
		 cout<<"\nCircumference = "<<2*r*pi;
		 cout<<"\nArea = "<<pi*(r*r);		 
 	    }
 	if(choice==4)
 		{cout<<"\Base? ";
 		 cin>>b;
 		 cout<<"\nHeight? ";//assuming its a right triangle, then the height is one of the sides 
 		 cin>>h;
 		 cout<<"\nLast Side? ";
 		 cin>>ls;
 		 cout<<"\nArea = "<<(b*h) / 2;
 		 cout<<"\nPerimeter = "<<b+h+ls;
 		}
   }while(choice!=5);
 cout<<"Thanks for using the calculator. ";
 cout<<"\n\n";
 return 0;  
}

Last edited on
What advantage would using an array give you over what you currently have?

About the sole advantage I can see is the ability to keep a history of what choices were made. The program would not be enhanced as written using an array.

Since the number of iterations is variable -- the user requests as many or as few calculations as they want -- I would use a std::vector over a regular sized-at-compile-time array.
https://en.cppreference.com/w/cpp/container/vector

One change I would make that could streamline the program is using a switch statement instead of so many ifs. A switch would make adding new choices much easier.
https://en.cppreference.com/w/cpp/language/switch

I would also declare PI as a constant since the value should not be alterable.
Thank you for the clarification. I seem to have mistaken arrays for switch statements. I was thinking about ways to use less ifs and thought the way to fix that was using arrays.

I will edit the question.
I already gave you a link about the switch statement. Here's a link to a tutorial about the statement you might find helpful:
https://www.learncpp.com/cpp-tutorial/53-switch-statements/

I could write the code for you, but you'll learn more if you make the attempt first yourself. If you are still stuck post the updated code and we can help.
Thanks for the resources. I'll give it a try
Topic archived. No new replies allowed.