Devin Dahlberg

C++ : Assignment Three

// This program asks for the user to choose whether they would like to calculate the area of a circle, rectangle, or triangle. Then asks for the variables for included in each equation, then outputs the area for the chosen shape.
//
//
//Program from Starting Out With C++ from Control Structures through Objects (Ninth Edition) by Tony Gaddis(2018) pg. 227, Problem #23.
//
// Programmed by Devin Dahlberg, CIS 251 Student
// January 29, 2024

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int choice;
	double length, width, base, height, radius, area, radiusSquared;
	

	//Allows the user to choose which shape they would like to find the area for.
	cout << "Geometry Calculator " << endl;
	cout << "    1. Calculate the Area of a Circle " << endl;
	cout << "    2. Calculate the Area of a Rectangle " << endl;
	cout << "    3. Calculate the Area of a Triangle " << endl;
	cout << "    4. Quit " << endl;
	cout << "    Enter your choice: ";
	cin >> choice;

	//If statement that determines calculating the area of a circle was chosen, then computes and prints the area.
	if (choice == 1)
	{
		cout << "What is the radius of the circle? ";
		cin >> radius;

		if (radius < 0)
		{
			cout << "The radius cannot be negative. Exiting the program... ";
			return 0;
		}

		radiusSquared = pow(radius,2);
		area = radiusSquared * 3.14159;

		cout << "The area of the circle is: " << area << "\n";
	}

	//If statement that determines calculating the area of a rectangle was chosen, then computes and prints the area.
	if (choice == 2)
	{
		cout << "What is the length of the rectangle? ";
		cin >> length;
		
		if (length < 0)
		{
			cout << "The length cannot be negative. Exiting the program... ";
			return 0;
		}

		cout << "What is the width of the rectangle? ";
		cin >> width;

		if (width < 0)
		{
			cout << "The width cannot be negative. Exiting the program... ";
			return 0;
		}

		area = length * width;

		cout << "The area of the rectangle is: " << area << "\n";
	}

	//If statement that determines calculating the area of a triangle was chosen, then computes and prints the area.
	if (choice == 3)
	{
		cout << "What is the base of the triangle? ";
		cin >> base;

		if (base < 0)
		{
			cout << "The base cannot be negative. Exiting the program... ";
			return 0;
		}

		cout << "What is the height of the triangle? ";
		cin >> height;

		if (height < 0)
		{
			cout << "The height cannot be negative. Exiting the program... ";
			return 0;
		}

		area = base * height * .5;

		cout << "The area of the triangle is: " << area << "\n";
	}

	// If statement that determines quit was chosen and exits the program.
	if (choice == 4)
	{
		cout << "Quitting the program.";
		return 0;
	}

	// If statement that determines if a choice is invalid then exits the program.
	if (choice <= 0 || choice >= 5) {
		cout << "Invalid choice. Exiting the program...";
		return 0;
	}

	return 0;
}

image

image

image

image

image

image