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.
#include <iostream>
#include <iomanip>
usingnamespace 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.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}