Answer to Question #124365 in C++ for wilfred

Question #124365
4. Develop a simple application using functions to calculate the area of the following shapes: Square, Rectangle and Triangle. The user should be able to perform any of the calculation and also press a key to terminate the program
1
Expert's answer
2020-06-29T08:19:09-0400
#include <iostream>

#include <thread>

using namespace std;




double square(double);

double square(double, double);

double square(double, double, double);

enum CHOICE { EXIT, CIRCLE, RECTANGLE, TRIANGLE};




void main()

{

double height, width, side_1, side_2, side_3, radius, area;

auto choice = 0;

while (choice == 1 || choice == 2 || choice == 3 || choice == 0)

{

cout << "\tMenu\n Press 1 to calculate the area of circle\n Press 2 to calculate the area of Rectangle\n Press 3 to calculate the area of triangle\n Press 0 to exit\n\tEnter your choice : ";

cin >> choice;

switch (choice)

{

case CIRCLE:

{

system("cls");

cout << "Enter radius of the circle : ";

cin >> radius;

try

{

cout << "Area = " << square(radius) << "\n";

}

catch (const char* message)

{

system("cls");

cout << message;

this_thread::sleep_for(2s);

system("cls");

}

break;

}

case RECTANGLE:

{

system("cls");

cout << "Enter height one of the rectangle: ";

cin >> height;

cout << "Enter width of the triangle: ";

cin >> width;

try

{

cout << "Area = " << square(height, width) << "\n";

}

catch (const char* message)

{

system("cls");

cout << message;

this_thread::sleep_for(2s);

system("cls");

}

break;

}

case TRIANGLE:

{

system("cls");

cout << "Enter side one of the triangle: ";

cin >> side_1;

cout << "Enter side two of the triangle: ";

cin >> side_2;

cout << "Enter side three of the triangle: ";

cin >> side_3;

try

{

cout << "\nArea of the Triangle = " << square(side_1, side_2, side_3) << "\n";

}

catch (const char* message)

{

system("cls");

cout << message;

this_thread::sleep_for(2s);

system("cls");

}

break;

}

case EXIT:

{

cout << "Exiting program";

exit(0);

}

default:

{

system("cls");

cout << "Wrong choice, try again";

choice = 0;

this_thread::sleep_for(1.5s);

system("cls");

break;

}

}

}

}




double square(double radius)

{

if (radius <= 0)

throw "radius can't be negative or zero";

return 3.14 * radius * radius;

}

double square(double height, double width)

{

if (height<=0 || width <=0)

throw "sides can't be negative or zero";

return height * width;

}

double square(double side_1, double side_2, double side_3)

{

double perimeter = (side_1 + side_2 + side_3) / 2;

if ((perimeter - side_1) <= 0 || (perimeter - side_2) <= 0 || (perimeter - side_3) <= 0)

{

throw "Triangle with such parameters does not exist!";

}

return sqrt(perimeter * (perimeter - side_1) * (perimeter - side_2) * (perimeter - side_3));

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS