Add a do-while loop to the program so that the user can repeatedly display the m
ID: 3820635 • Letter: A
Question
Add a do-while loop to the program so that the user can repeatedly display the menu, make a choice, and have the appropriate steps for that choice carried out. The loop should continue iterating until the user enters 4 for the menu choice. Have the program print several blank lines after each case is carried out before the menu displays again. Once the program compiles with no errors, test it with a single run that tries all menu choices before entering 4 to quit. Design and implement the areas.cpp program so that it correctly meets the program specifications given below. Specifications: Create a menu-driven program that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1--square 2--circle 3--right triangle 4--quit If the user selects choice 1, the program should find the area of a square. If the user selects choice 2, the program should find the area of a circle. If the user selects choice 3, the program should find the area of a right triangle. If the user selects choice 4, the program should quit without doing anything. If the user selects anything else (i.e., an invalid choice) an appropriate error message should be printed Sample Run Program to calculate areas of objects 1--square 2--circle 3--right triangle 4--quit Radius of the circle: 3.0 Area = 28.2743Explanation / Answer
#include <iostream>
#define PI 3.14
using namespace std;
double circleArea(double radius)
{
return PI*radius*radius;
}
double squareArea(double side)
{
return side*side;
}
double triangleArea(double base, double height)
{
return base*height/2;
}
void printMenu()
{
cout << endl<<endl;
cout << "Program to calculate areas of objects" << endl;
cout << " 1 -- square 2 -- circle 3 -- right triangle 4 -- quit" << endl;
}
int main()
{
while(true)
{
int choice;
printMenu();
cin >> choice;
if (choice == 1)
{
cout << "Side of Square: ";
double side;
cin >> side;
cout << "Area = " << squareArea(side) << endl;
}
else if (choice == 2)
{
cout << "Radius of circle: ";
double radius;
cin >> radius;
cout << "Area = " << circleArea(radius) << endl;
}
else if (choice == 3)
{
cout << "Base of right angle triangle: ";
double base;
cin >> base;
cout << "Height of right angle triangle: ";
double height;
cin >> height;
cout << "Area = " << triangleArea(base, height) << endl;
}
else if (choice == 4)
{
break;
}
else
{
cout << "Please choose from given menu!" << endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.