Respuesta :
Answer & Explanation:
This program is written in C++ and it combines the step 1 and step 2 to create a menu driven program
Each line makes use of comments (as explanation)
Also, see attachment for program file
Program Starts Here
//Put Your Name Here; e.g. MrRoyal
//This program calculates the area of circle, triangle and square; depending on the user selection
//The next line include necessary header file
#include<iostream>
using namespace std;
int main()
{
//The next line defines variable pi as a constant with float datatype
const float pi = 3.14159;
// The next two lines declares all variables that'll be needed in the program
string choice;
float length, base, height, radius, area;
// The next four lines gives an instruction to the user on how to make selection
cout<<"Press 1 to calculate area of a square: "<<endl;
cout<<"Press 2 to calculate area of a circle: "<<endl;
cout<<"Press 3 to calculate area of a triangle: "<<endl;
cout<<"Press 4 to quit: "<<endl;
//This next line prompts user for input
cout<<"Your choice: ";
//This next line gets user input and uses it to determine the next point of execution
cin>>choice;
if(choice == "1") //If user input is 1, then the choice is area of square
{
cout<<"Length: "; //This line prompts user for length of the square
cin>>length; // This line gets the length of the square
area = length * length; //This line calculates the area
cout<<"Area: "<<area; //This line prints the calculated area
}
else if(choice == "2") //If user input is 2, then the choice is area of circle
{
cout<<"Radius: "; //This line prompts user for radius
cin>>radius; //This line gets radius of the circle
area = pi * radius * radius; // This line calculates the area
cout<<"Area: "<<area; //This line prints the calculated area
}
else if(choice == "3") //If user input is 2, then the choice is area of triangle
{
cout<<"Base: "; //This line prompts user for base
cin>>base; //This line gets the base
cout<<"Height: "; //This line prompts user for height
cin>>height; //This line gets the height
area = 0.5 * base * height; //This line calculates the area
cout<<"Area: "<<area; //This line prints the calculated area
}
else if(choice == "4") //If user input is 4, then the choice is to quit
{
//Do nothing and quit
}
else //Any other input is invalid
{
cout<<"Invalid Option Selected";
}
return 0;
}