C++ Program: Redesign the program that calculates the area of geometric shapes.
ID: 3862903 • Letter: C
Question
C++ Program: Redesign the program that calculates the area of geometric shapes. Create a function call for each selection the user makes.
Use these as the function declaration:
//function declaration
void showMenu();
double rectangle(double length, double width);
double triagle(double base, double height);
double circle(double radius);
Here is the source code to calculate the area of geometric shapes:
#include <iostream>
#include <iomanip> //allows decimal points to be shown (setprecision)
using namespace std;
int main()
{
int choice; //To hold a menu choice
double length, //holds the stuff needed to caluclate areas
width,
base,
height,
r;
double area;
//constants for menu choices
const int AREA_RECTANGLE = 1,
AREA_TRIANGLE = 2,
AREA_CIRCLE = 3,
QUIT = 4;
//set up reputation
do
{
//display the menu and get choice
cout << " Geometry Calculator Menu ";
cout << "1. Calculate the Area of a Rectangle. ";
cout << "2. Calculate the Area of a Triangle. ";
cout << "3. Calculate the Area of a Circle. ";
cout << "4. Quit. ";
cout << "Enter your choice (1, 2, 3, or 4): ";
cin >> choice;
//set numeric outpoint formatting
cout << fixed << showpoint << setprecision(2);
//respond to the user's menu selection
switch (choice)
{
case AREA_RECTANGLE: //choice 1 - rectangle
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
//only calculate area if user enters positive numbers
if ((length > 0) && (width > 0))
{
//calculate the area
area = length * width;
//display the area
cout << "The area of the rectangle is " << area << ". ";
}
else
cout << "Please enter positive numbers only. " << endl;
break;
case AREA_TRIANGLE: //choice 2 - triangle
cout << "Enter the base of the triangle: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;
//only calculate area if user enters positive numbers
if ((base > 0) && (height > 0))
{
//calculate the area
area = base * height *.5;
//display the area
cout << "The area of the triangle is " << area << ". ";
}
else
cout << "Please enter positive numbers only. " << endl;
break;
case AREA_CIRCLE: //choice 3 - circle
cout << "Enter the radius of the circle: ";
cin >> r;
//only calculate area if user enters positive numbers
if (r > 0)
{
//calculate the area
area = 3.14159 * (r * r);
//display the area
cout << "The area of the circle is " << area << ". ";
}
else
cout << "Please enter a positive number for the radius. " << endl;
break;
case QUIT: //choice 4 - quit program
cout << "Program will end. ";
}
} while (choice != QUIT);
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip> //allows decimal points to be shown (setprecision)
using namespace std;
//function declaration
void showMenu();
double rectangle(double length, double width);
double triagle(double base, double height);
double circle(double radius);
int main()
{
int choice; //To hold a menu choice
double length, //holds the stuff needed to caluclate areas
width,
base,
height,
r;
double area;
//constants for menu choices
const int AREA_RECTANGLE = 1,
AREA_TRIANGLE = 2,
AREA_CIRCLE = 3,
QUIT = 4;
//set up reputation
do
{
showMenu();
cin >> choice;
//set numeric outpoint formatting
cout << fixed << showpoint << setprecision(2);
//respond to the user's menu selection
switch (choice)
{
case AREA_RECTANGLE: //choice 1 - rectangle
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
//only calculate area if user enters positive numbers
if ((length > 0) && (width > 0))
{
//calculate the area
area = rectangle(length, width);
//display the area
cout << "The area of the rectangle is " << area << ". ";
}
else
cout << "Please enter positive numbers only. " << endl;
break;
case AREA_TRIANGLE: //choice 2 - triangle
cout << "Enter the base of the triangle: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;
//only calculate area if user enters positive numbers
if ((base > 0) && (height > 0))
{
//calculate the area
area = triagle( base, height);
//display the area
cout << "The area of the triangle is " << area << ". ";
}
else
cout << "Please enter positive numbers only. " << endl;
break;
case AREA_CIRCLE: //choice 3 - circle
cout << "Enter the radius of the circle: ";
cin >> r;
//only calculate area if user enters positive numbers
if (r > 0)
{
//calculate the area
area = circle(r);
//display the area
cout << "The area of the circle is " << area << ". ";
}
else
cout << "Please enter a positive number for the radius. " << endl;
break;
case QUIT: //choice 4 - quit program
cout << "Program will end. ";
}
} while (choice != QUIT);
return 0;
}
void showMenu() {
//display the menu and get choice
cout << " Geometry Calculator Menu ";
cout << "1. Calculate the Area of a Rectangle. ";
cout << "2. Calculate the Area of a Triangle. ";
cout << "3. Calculate the Area of a Circle. ";
cout << "4. Quit. ";
cout << "Enter your choice (1, 2, 3, or 4): ";
}
double rectangle(double length, double width) {
return length * width;
}
double triagle(double base, double height){
return base * height *.5;
}
double circle(double radius) {
return 3.14159 * (radius * radius);
}
Output:
Enter your choice (1, 2, 3, or 4): 1
Enter the length of the rectangle: 2
Enter the width of the rectangle: 3
The area of the rectangle is 6.00.
Geometry Calculator Menu
1. Calculate the Area of a Rectangle.
2. Calculate the Area of a Triangle.
3. Calculate the Area of a Circle.
4. Quit.
Enter your choice (1, 2, 3, or 4): 2
Enter the base of the triangle: 2
Enter the height of the triangle:
3
The area of the triangle is 3.00.
Geometry Calculator Menu
1. Calculate the Area of a Rectangle.
2. Calculate the Area of a Triangle.
3. Calculate the Area of a Circle.
4. Quit.
Enter your choice (1, 2, 3, or 4): 3
Enter the radius of the circle: 4
The area of the circle is 50.27.
Geometry Calculator Menu
1. Calculate the Area of a Rectangle.
2. Calculate the Area of a Triangle.
3. Calculate the Area of a Circle.
4. Quit.
Enter your choice (1, 2, 3, or 4): 4
Program will end.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.