Step1: This file contains just a program shell in which you will write all the programming statements needed to complete the program described below. Here is a sample of the current contents of areas.cpp 1 // Assignment 5 is to compute the area (s) WRITE A COMMENT BRIEFLY DESCRIBING THE PROGRAM PUT YOUR NAME HERE. 2 3 4 // INCLUDE ANY NEEDED HEADER FILES HERE 5 using namespace std;l 7 int main) 9// DEFINE THE NAMED CONSTANT PI HERE AND SET ITS VALUE TO 3.14159 10 11 // DECLARE ALL NEEDED VARIABLES HERE. GIVE EACH ONE A DESCRIPTIVE 12// NAME AND AN APPROPRIATE DATA TYPE 13 14// WRITE STATEMENTS HERE TO DISPLAY THE 4 MENU CHOICES 15 16// WRITE A STATEMENT HERE TO INPUT THE USERS MENU CHOICE 17 18// WRITE STATEMENTS TO OBTAIN ANY NEEDED INPUT INFORMATION 19// AND COMPUTE AND DISPLAY THE AREA FOR EACH VALID MENU CHOICE 20 / IF AN INVALID MENU CHOICE WAS ENTERED, AN ERROR MESSAGE SHOULD 21 /BE DISPLAYED 23 return 0 24 )
Step 2: Design and implement the areas.cpp program so that it correctly meets the program specifications given below Specifications: Sample Run Program to calculate areas of objects 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 1square 2 -- circle 3 -- right triangle 4quit Radius of the circle: 3.0 Area 28.2743 . If the user selects choice 1, the program should find the area of a square . If the user selects choice 2, the program should . If the user selects choice 3, the program should . If the user selects choice 4, the program should . If the user selects anything else (i.e., an invalid find the area of a circle find the area of a right triangle quit without doing anything choice) an appropriate error message should be printed

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;

}

Ver imagen MrRoyal