Switch Statements, and do, while loops

I have to do this assignment:
Write a menu driven program (that means Switch statements) that displays the following menu:
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1- 4):

My TA told me to use a do while loop
This is my code, and it wont work at all, does anybody have any tips on how to fix this, or a better way to approach this problem? I am just starting c++ so please use beginner techniques.

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
  #include <iostream> 
#include <iomanip>      
                
using namespace std;

int main()
{
	int choice;
	double radius,pi,length,width,base,height;
	
	pi=3.14159;
	do
	{
		cout<<"1. Calculate the Area of a Circle";
		cout<<"2. Calculate the Area of a Rectangle";
		cout<<"3. Calculate the Area of a Triangle";
		cout<<"quit";
		cout<<Enter 1,2,3, or 4: ";
		cin<<choice;
		
	while(choice>4 || choice<1)
	{
		cout<<"Please enter a valid choice";
		cin<<choice;
	}
	while(choice<=4 && choice>=1)
	{
		switch(choice)
		{
			case 1: cout<<"what is the radius of the circle?";
			cin>>radius;
			area=pi*pow(radius, 2.00)
			cout<<"The area of the circle is "<<area;
			case 2: cout<<"what is the length of the rectangle? ";
			cin>>length;
			cout<<"what is the width of the rectangle? ";
			cin>>width;
			area=length*width;
			cout<<"The area of the rectangle is "<<area;
			case 3: cout<<"What is the length of the triangle's b-ase? ";
			cin<<base;
			cout<<"What is the height of the triangle? ";
			cin<<height;
			area=(1/2)*base*height;
			cout<<"The area of the triangle is "<<area;
			case 4: cout<<"goodbye";
		}
	}	
	}
	
	return 0;
} 
I don't know what IDE (integrated development environment) you are using but I know if you stick your code in Visual Studio you have a lot of errors and most are underlined in red lines which show you something is wrong. Except for a lot of errors, your code is not too far off what you are trying to achieve. You need to look closely at where you put {}. With your logic, you may need to walk through your program on a piece of paper doing just as you have it set up to do and look and see where it goes wrong.

Here is your code reworked to work. Take a good look and compare the two. Some of the things I changed are marked.

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

using namespace std;

int main()
{
	int choice;
	double radius, pi, length, width, base, height, area; //<--added area

	pi = 3.14159;
	do
	{
		cout << "What would you like to do?" << endl;
		cout << "1. Calculate the Area of a Circle" << endl;
		cout << "2. Calculate the Area of a Rectangle" << endl;
		cout << "3. Calculate the Area of a Triangle" << endl;
		cout << "4. Quit" << endl; //<-- added 4 so user knows what to hit to quit
		cout << "Enter 1, 2, 3, or 4: " << endl; //<-- added a: "
		cin >> choice; //<-- cin use: >>

		while (choice < 1 || choice > 4)
		{
			cout << "Please enter a valid choice" << endl;
			cout << "Enter 1, 2, 3, or 4: " << endl;
			cin >> choice;
		}
	


		switch (choice)
		{
		case 1:
			cout << "What is the radius of the circle?" << endl;
			cin >> radius;
			area = pi*pow(radius, 2.00); //<-- added a: ; and area needed to be decalared a variable
			cout << "The area of the circle is " << area << endl;
			break;
		case 2:
			cout << "What is the length of the rectangle? " << endl;
			cin >> length;
			cout << "What is the width of the rectangle? " << endl;
			cin >> width;
			area = length*width;
			cout << "The area of the rectangle is " << area << endl;
			break;
		case 3:
			cout << "What is the length of the triangle's b-ase? " << endl;
			cin >> base; //<-- cin use: >>
			cout << "What is the height of the triangle? " << endl;
			cin >> height; //<-- cin use: >>
			area = .5*(base*height);
			cout << "The area of the triangle is " << area << endl;
			break;
		case 4: cout << "Goodbye" << endl;
		}
	} while (choice <= 3 && choice >= 1);

	return 0;
}
joe864864,

This really helped me, thank you so much!
Glad it helped!
Topic archived. No new replies allowed.