Question

#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...

#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
***************************************************************/
  1. 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

    • its name
    • its use or function: that is, what does it do? What service does it provide to the code that calls it?
    • a list of its arguments briefly describing the meaning and use of each
    • the value returned (if any) or none
    • notes on any unusual features, assumptions, techniques, etc.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout...
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout << "Welcome to the Calorie Count-ulator!\n"; // Get user input: cout << "Enter your meal choice ([P]izza, [S]alad, [H]amburger)\n"; cin >> meal_choice; cout << "Enter the amount of servings (1-9):\n"; cin >> servings; // TODO: Use a switch statement to evaluate the user's meal choice // Handle error checking where appropriate // Exit the program: return 0;}
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) // declaration of main...
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) // declaration of main function { int limit, // a user entered limit for a loop step, // a user entered step value x; // control variable for the loop cout << "Counting up from 1 to 10\n"; // The control variable x takes on values from 1 to 10 for (x = 1; x <= 10; x++) // for loop to count from 1 to 10 values...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...