CSCI 240 Spring 2025

Assignment 5
Loops and Decision Statements
(100 points)


Due: Friday, February 21 February 28 on the autograder and Blackboard by 11:59 PM

Overview

For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue.

The cpp file that is submitted for grading must be named assign5.cpp.

Basic Program Logic

The program should start by displaying a menu:

Geometry Calculator

1. Calculate the area of a circle
2. Calculate the area of a triangle
3. Calculate the area of an ellipse
4. Quit

Enter your choice(1-4):

and get the user's choice as an integer.

After the choice has been made, if the quit option was not chosen, the program should enter a loop that will continue until the quit option is chosen.

If the circle option (1) was selected, the program should prompt to enter the radius of a circle (integer). The value should then be used to calculate the area of a circle using the formula:

 Area of a Circle = Πr2

where r is the radius of the circle. Use the value 3.14159 as the value for pi.

If the triangle option (2) was selected, the program should prompt to enter the length of the triangle’s base (integer) and and its height (integer). The values should then be used to calculate the area of a triangle using the formula:

Area of a Triangle = 1/2 * base * height

If the ellipse option (3) was selected, the program should prompt the user for the length of the semi-major axis (integer) (this is half the length of the widest diameter of the ellipse) and the length of the semi-minor axis (integer) (this is half the length of the narrowest diameter of the ellipse) of the ellipse. The values should then be used to calculate the area of an ellipse using the formula:

Area of Ellipse = Π * length of Semi-Major Axis * length of Semi-Minor Axis

If the user entered an invalid option (something other than 1, 2, 3, or 4), the program should display an error message about an invalid choice being made.

At the end of the loop, the menu should be displayed to the user again and their new choice should be read.

Symbolic Constants

This program requires the use of at least FIVE symbolic constants.

The first constant is a double that represents the value PI. The value should be 3.14159.

The second constant is an integer that represents the menu option for a circle. The value should be 1.

The third constant is an integer that represents the menu option for a triangle. The value should be 2.

The fourth constant is an integer that represents the menu option for an ellipse. The value should be 3.

The fifth constant is an integer that represents the menu option for quitting. The value should be 4.

More symbolic constants may be added to the code if necessary.

Program Requirements

  1. The numeric values read in from the user should all be data type integer. The calculated areas should all be data type double. Make sure to use meaningful variable names.

  2. The program MUST use the five symbolic constants that are specified above.

  3. The areas should be displayed with exactly 3 digits after the decimal point.

  4. Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.

Formatting Help

  1. The menu should start with exactly one newline character.

  2. The prompts for the radius of the circle, length of the triangle base, and the length of the semi-major axis of the ellipse should start with exactly one newline character. The other prompts (height of the triangle and semi-minor axis of the ellipse) DO NOT have a newline character at the beginning.

  3. The display of the calculated areas should start and end with exactly one newline character. They should all follow the format "The area of the [shape] is [area]" where [shape] is replaced with circle, triangle, or ellipse and [area] is replaced by the calculated area.

  4. The error message about invalid selection should start with exactly one newline character and include the invalid choice. It should follow the format "*** ERROR: [choice] is an invalid selection ***" where [choice] is replaced by the invalid selection.

Prompts

  1. What is the radius of the circle?

  2. What is the length of the base of the triangle?

  3. What is the height/altitude of the triangle?

  4. What is the length of the semi-MAJOR axis of the ellipse?

  5. What is the length of the semi-MINOR axis of the ellipse?

Output

Geometry Calculator

1. Calculate the area of a circle
2. Calculate the area of a triangle
3. Calculate the area of an ellipse
4. Quit

Enter your choice (1-4): 1

What is the radius of the circle? 3

The area of the circle is 28.274

Geometry Calculator

1. Calculate the area of a circle
2. Calculate the area of a triangle
3. Calculate the area of an ellipse
4. Quit

Enter your choice (1-4): 2

What is the length of the base of the triangle? 8
What is the height/altitude of the triangle? 10

The area of the triangle is 40.000

Geometry Calculator

1. Calculate the area of a circle
2. Calculate the area of a triangle
3. Calculate the area of an ellipse
4. Quit

Enter your choice (1-4): 3

What is the length of the semi-MAJOR axis of the ellipse? 12
What is the length of the semi-MINOR axis of the ellipse? 9

The area of the ellipse is 339.292

Geometry Calculator

1. Calculate the area of a circle
2. Calculate the area of a triangle
3. Calculate the area of an ellipse
4. Quit

Enter your choice (1-4): 9

*** ERROR: 9 is an invalid selection ***

Geometry Calculator

1. Calculate the area of a circle
2. Calculate the area of a triangle
3. Calculate the area of an ellipse
4. Quit

Enter your choice (1-4): -4

*** ERROR: -4 is an invalid selection ***

Geometry Calculator

1. Calculate the area of a circle
2. Calculate the area of a triangle
3. Calculate the area of an ellipse
4. Quit

Enter your choice (1-4): 4