#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice is triangle else if(choice==2) { cout<<"What is the length of the base of the triangle ? "; cin>>base; cout<<"What is the height/altitude of the triangle ? "; cin>>height; //calculating area area=0.5*base*height; cout<<endl<<"The area of the triangle is "<<fixed<<setprecision(3)<<area<<endl; } //if user wants to quit else if(choice==3) { cout<<"Goodbye!"; //exiting from loop break; } //if wrong choice else { cout<<endl<<"*** Error: "<<choice<<" is an invalid selection ***"<<endl; } } return 0; }
Overview
For this assignment, re-write the program above so that it uses functions rather than having all of the code in int main().
Basic Program Logic
As with the program above, this version of the geometry calculator program will start by displaying a menu to the user and getting their choice as an integer. However, this will be done by calling the menu() function that is described below rather than having the cout/cin in main().
After the user's choice has been made, if the user did not choose the quit option, the program should enter a loop that will continue until the user wants to quit.
If the user chose the circle option (1), call the getPositiveInt() function that is described below to prompt the user to enter the radius of a circle. The value that is returned from getPositiveInt() should then be used to calculate the area of a circle using the formula from the program above. After the area has been calculated, call the displayArea() function to display the calculated area of the triangle.
If the user chose the triangle option (2), call the getPositiveInt() function to prompt the user to enter the length of the triangle’s base and then call the getPositiveInt() function a second time to prompt the user to enter the height of the triangle. The two values that are returned from the two getPositiveInt() calls should then be used to calculate the area of a triangle using the formula from the program above. After the area has been calculated, call the displayArea() function to display the calculated area of the triangle.
At the end of the loop, call the menu() function (again) to display the menu to the user and get their new choice.
Note 1: the check for an invalid menu option has been removed from main() in this program because it will now be handled in the menu() function.
The Functions
Write and use the following 3 functions in the program.
int menu()
This function will display a menu and get a VALID choice from the user. It takes no arguments. It returns an integer: the valid menu choice.
The function should display the menu from the program above to the user and then get the user's choice. The user's choice should then be checked to make sure it's valid (1, 2, 3). As long as the user has entered an invalid choice, an error message should be displayed and the user should be given a chance to re-enter their choice. Once the user has entered a valid choice, it should be returned.
int getPositiveInt( string prompt )
This function will get a positive integer from the user. It takes one argument: a string that contains the prompt that should be displayed to the user. It returns an integer: the positive value that is entered by the user.
The function should simply display the string argument (prompt) to the user and then get the user's integer value. The user's value should then be checked to make sure it's positive. As long as the user has entered a negative value or 0, an error message should be displayed and the user should be given a chance to re-enter their value. Once the user has entered a positive integer value, it should be returned.
void displayArea( string label, double area )
This function will display a calculated area. It takes two arguments: a string that contains the label that should be displayed with the area and a double that contains the area to be displayed. It returns nothing.
The function should simply display the string (label) and double (area) arguments to the user in a formatted manner. The area should be displayed with exactly 3 digits after the decimal point.
Program Requirement
Each function must have a documentation box explaining:
/*************************************************************** Function: int menu() Use: This function displays a menu to the user and gets their choice. Arguments: None Returns: integer - the user's choice from the menu Note: The user's choice is checked to make sure that it is valid ***************************************************************/
As with the previous assignments and the assignments until the end of the semester, complete program documentation is required. For this assignment, that means that line documentation AND function documentation boxes are needed. In regards to line documentation, there is no need to document every single line, but logical "chunks" of code should be preceded by a line or two that describe what the "chunk" of code does. Make sure that main() and any function that you write contains line documentation
Program:
#include<iostream>
#include<iomanip>
using namespace std;
int menu()
{
int ch;
//menu
cout<<endl<<endl<<"Geometry
Calculator"<<endl<<endl;
cout<<"1. Calculate the area of a circle"<<endl;
cout<<"2. Calculate the area of a
triangle"<<endl;
cout<<"3. Quit"<<endl<<endl;
do{
//prompt for choice
cout<<"Enter your choice(1-3): ";
cin>>ch;
cout<<endl;
if(ch<1 || ch>3)
{
cout<<"Invalid choice, try again";
}
}while(ch<1 || ch>3);
return ch;
}
int getPositiveInt(string userInput)
{
int n;
cout<<userInput;
cin>>n;
return n;
}
void displayArea(string label, double area)
{
cout<<label<<fixed<<setprecision(3)<<area<<endl;
}
int main()
{
//variables
int choice;
float area;
const double PI=3.14159;
choice = menu();
//repeat until user wants to quits
while(true)
{
//if choice is circle
if(choice==1)
{
int radius = getPositiveInt("What is the radius of the circle?
");
//calculating area
area=PI*radius*radius;
displayArea("The area of the circle is ",area);
choice = menu();
}
//if choice is triangle
else if(choice==2)
{
int base = getPositiveInt("What is the length of the base of the
triangle ? ");
int height = getPositiveInt("What is the height/altitude of the
triangle ? ");
//calculating area
area=0.5*base*height;
displayArea("The area of the triangle is ",area);
choice = menu();
}
//if user wants to quit
else if(choice==3)
{
cout<<"Goodbye!";
//exiting from loop
break;
}
//if wrong choice
else
{
cout<<endl<<"*** Error: "<<choice<<" is an
invalid selection ***"<<endl;
}
}
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.