Write a program that displays the following menu: Geometry Calculator 1. Calcula
ID: 3614521 • Letter: W
Question
Write a program that displays the following menu: Geometry Calculator 1. Calculate the Area of aCircle 2. Calculate the Area of aRectangle 3. Calculate the Area of aTriangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius ofthe circle and then display its area. Use the followingformula: area = (pi)r squared 2 Use 3.14159 for (pi) and the radius of the circle for r. Ifthe user enters 2, the program should ask for the length and widthof the rectangle and then display the rectangle's area. Use thefollowing formula: area = length * width If the user enters 3 the program should ask for the length ofthe triangle's base and its height, and then display its area. Usethe following formula: area = base * height * .5 If the user enters 4, the program should end Input Validation: Display an error message if the userenters a number outside the range of 1 through 4 when selecting anitem from the menu. Do not accept negative values for the circle'sradius, the rectangle's length or width, or the triangle's base orheight. Write a program that displays the following menu: Geometry Calculator 1. Calculate the Area of aCircle 2. Calculate the Area of aRectangle 3. Calculate the Area of aTriangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius ofthe circle and then display its area. Use the followingformula: area = (pi)r squared 2 Use 3.14159 for (pi) and the radius of the circle for r. Ifthe user enters 2, the program should ask for the length and widthof the rectangle and then display the rectangle's area. Use thefollowing formula: area = length * width If the user enters 3 the program should ask for the length ofthe triangle's base and its height, and then display its area. Usethe following formula: area = base * height * .5 If the user enters 4, the program should end Input Validation: Display an error message if the userenters a number outside the range of 1 through 4 when selecting anitem from the menu. Do not accept negative values for the circle'sradius, the rectangle's length or width, or the triangle's base orheight.Explanation / Answer
#include<iostream>
using namespace std;
int main()
{int choice=0;
double pi=3.14159,area,val1=-1,val2=-1;
for(; ;)
{
while(choice<1||choice>4)
{cout<<" 1. Calculate the Area of aCircle ";
cout<<"2. Calculate the Areaof a Rectangle ";
cout<<"3. Calculate the Areaof a Triangle ";
cout<<"4. Quit ";
cout<<"Enter your choice (1-4): ";
cin>>choice;
if(choice<1||choice>4)
cout<<"Invalidentry-please reenter ";
}
if(choice==1)
{while(val1<0)
{cout<<"Enter radius: ";
cin>>val1;
if(val1<0)
cout<<"invalid entry-please reenter ";
}
cout<<"Area ="<<pi*(val1*val1)<<endl;
}
else if(choice==2)
{while(val1<0)
{cout<<"Enter length: ";
cin>>val1;
if(val1<0)
cout<<"invalid entry-please reenter ";
}
while(val2<0)
{cout<<"Enter width: ";
cin>>val2;
if(val2<0)
cout<<"invalid entry-please reenter ";
}
cout<<"Area= "<<val1*val2<<endl;
}
else if(choice==3)
{while(val1<0)
{cout<<"Enter length: ";
cin>>val1;
if(val1<0)
cout<<"invalid entry-please reenter ";
}
while(val2<0)
{cout<<"Enter height: ";
cin>>val2;
if(val2<0)
cout<<"invalid entry-please reenter ";
}
cout<<"Area ="<<val1*val2*.5<<endl;
}
else
return 0;
choice=0;
val1=-1;
val2=-1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.