| CSCI 240 | Spring 2026 |
For this assignment, write a menu-based program that will act as an area calculator for various shapes.
The shapes for this program are a circle, triangle, and trapezoid.
The cpp file that is submitted for grading must be named assign3.cpp.
The basic concept for this program is like what has been done in assignments 1 and 2: the user is asked to enter values, a calculation is performed, and the result of the calculation is displayed. The new concept is that different input values and calculations will be asked for and performed based on the user's menu choice.
Start by displaying a menu to the user that presents the different shapes that are available. The user's choice should be stored in an integer variable.
The menu:
Area Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Calculate the area of a trapezoid What is your choice (1-3)?
There should be exactly 1 space after the question mark.
Use a cascading decision statement to make sure that the area is calculated for only a single shape and to do error checking of the user's choice from the menu.
For option 1, the program should calculate the area of a circle. Ask the user for the radius of the circle (an integer). Use a decision statement to verify the radius value. If the radius is less than or equal to 0, display an error message to the user and ask them to re-enter the radius value. (Note: this type of error checking will give the user one chance to enter a valid value after they've made an error. It is not the best type of error checking because it is possible the user makes another error. For this assignment, that's okay. We'll do better error checking in future assignments.) Use the valid radius value entered by the user to calculate the area of the circle. Display the calculated area in the format.
For option 2, the program should calculate the area of a triangle. Ask the user for the length of the base of a triangle (an integer). Use a decision statement to verify the length value. If the length value is less than or equal to 0, display an error message to the user and ask them to re-enter the value. Ask the user for the height of the triangle (an integer). Use a decision statement to verify the height value. If the height value is less than or equal to 0, display an error message to the user and ask them to re-enter the value. Use the length of the base and the height of the triangle to calculate the area of the triangle. Display the calculated area.
For option 3, the program should calculate the area of a trapezoid. Ask the user for the length of the top of the trapezoid (an integer). Use a decision statement to verify the length of the top. If the length value is less than or equal to 0, display an error message to the user and ask them to re-enter the value. Ask the user for the length of the bottom of the trapezoid (an integer). Use a decision statement to verify the length of the bottom. If the length value is less than or equal to 0, display an error message to the user and ask them to re-enter the value. Ask the user for the height of the trapezoid (an integer). Use a decision statement to verify the height value. If the height value is less than or equal to 0, display an error message to the user and ask them to re-enter the value. Use the two length values and height to calculate the area of the trapezoid. Display the calculated area.
For anything other than 1 through 3, the program should display an error message that says the choice is not valid.
area of a circle = 𝜋r2
area of a triangle = 1/2 * length of the base of the triangle * height of the triangle
area of a trapezoid = 1/2 * (length of top + length of bottom) * height
The prompts to the user should be:
"What is the radius of the circle?"
"What is the length of the triangle base?"
"What is the height of the triangle?"
"What is the length of the top of the trapezoid?"
"What is the length of the bottom of the trapezoid?"
"What is the height of the trapezoid?"
There should be a newline character at the beginning of the prompt and exactly 1 space after the question mark.
The error message format for invalid input should be:
[incorrect value] is an invalid value. It must be greater than 0. Try again:
where the [incorrect value] is replaced by the invalid value entered by the user. There should be a newline character before the incorrect value and exactly 1 space after the colon (:).
The format for the area displays should be:
The area of a circle with radius [radius] is [area]
The area of a triangle with a base of [base] and height of [height] is [area]
The area of a trapezoid with a top length of [top], bottom length of [bottom], and height of [height] is [area]
where [radius], [base], [height], [top], and [bottom] are replaced by the values entered by the user, and [area] is replaced by the calculated area of the shape There should be a newline character at the beginning and end of the line.
The error message format for invalid menu choice should be:
[invalid choice] is an invalid choice from the menu
where the [invalid choice] is replaced by the invalid menu choice entered by the user. There should be a newline character at the beginning and end of the line.
At the top of the C++ source code, include a documentation box that resembles the one from programs 1 and 2. This will be a part of every program that is submitted during the semester and this will be the last reminder in the program write-ups.
Include 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 describes what the "chunk" of code does. This will also be a part of every program that is submitted for the remainder of the semester and this will be the last reminder in the program write-ups.
All the calculated areas should be displayed with exactly 3 digits after the decimal point.
The numeric values read in from the user should all be integer values. The calculated areas should all be float values. Use meaningful variable names.
Make sure and test the program with values other than what is shown in the output.
Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.
A few runs of the program should produce the following results:
Area Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Calculate the area of a trapezoid Enter your choice (1-3): 7 7 is an invalid choice from the menu
Area Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Calculate the area of a trapezoid Enter your choice (1-3): 1 What is the radius of the circle? 13 The area of a circle with radius 13 is 530.929
Area Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Calculate the area of a trapezoid Enter your choice (1-3): 1 What is the radius of the circle? -4 -4 is an invalid value. It must be greater than 0. Try again: 8 The area of a circle with radius 8 is 201.062
Area Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Calculate the area of a trapezoid Enter your choice (1-3): 2 What is the length of the triangle base? -9 -9 is an invalid value. It must be greater than 0. Try again: 9 What is the height of the triangle? -2 -2 is an invalid value. It must be greater than 0. Try again: 11 The area of a triangle with a base of 9 and height of 11 is 49.500
Area Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Calculate the area of a trapezoid Enter your choice (1-3): 3 What is the length of the top of the trapezoid? -1 -1 is an invalid value. It must be greater than 0. Try again: 6 What is the length of the bottom of the trapezoid? 0 0 is an invalid value. It must be greater than 0. Try again: 10 What is the height of the trapezoid? -7 -7 is an invalid value. It must be greater than 0. Try again: 7 The area of a trapezoid with a top length of 6, bottom length of 10, and height of 7 is 56.000