Now my assignment is to write a C++ program that prompts user to enter first character of a square or a triangle (s/t).
It should then calculate area of square (area=side * side)
or area of a triangle (1/2 * base* height) depending on the choice made by the user.
The area of the figure selected will be displayed as result to user.
It should be implemented using functions and an if statement.
Here's the thing. I coded a good chunk of it but i'm struggling with calling the functions and using an if statement. I feel like I'm also overthinking it, all i need is a push in the right direction.
#include <iostream>
#include <cmath>
usingnamespace std;
float tri_area(float, float);
float squ_area(float, float);
int main ()
{
int t, // input: t - triangle
s; // input: s - square
// prompting user to decide if they want the area of a triangle or square calculated.
cout << "Please enter the type of shape, (t) for triangle and (s) for square: "; cin >>;
return 0;
}
float tri_area (float, float)
{
float base,
height;
cout << "Input the base of the triangle: " ; cin >> base;
cout << "Input the height of the triangle: "; cin >> height;
float tri_area = 1/2 * base * height;
cout << "The area of the triangle is: " << tri_area << endl;
}
float squ_area (float, float)
{
float area,
side1,
side2;
cout << "Input Side 1: "; cin >> side1;
cout << "Input Side 2: "; cin >> side2;
float squ_area = side1 * side2;
cout << "The area of the square is: " << squ_area << endl;
}
cout << "Please enter the type of shape, (t) for triangle and (s) for square: ";
char type;
cin >> type;
if (type=='t'){
//code for the triangle
}
elseif (type=='s'){
//code for the square
}
else
cout << "invalid input\n";
#include <iostream>
usingnamespace std;
void tri_area();
void squ_area();
int main ()
{
char type;
cout << "Please enter the type of shape, (t) for triangle and (s) for square: "; cin >>type;
if(type=='t')
tri_area();
elseif(type=='s')
squ_area();
else
cout<<"Enter correct option!!"<<endl;
return 0;
}
void tri_area ()
{
float base,height,triangle_area;
cout << "Input the base of the triangle: " ; cin >> base;
cout<<endl;
cout << "Input the height of the triangle: "; cin >> height;
cout<<endl;
triangle_area= ((1/2 )* base * height);
cout << "The area of the triangle is: " << triangle_area << endl;
}
void squ_area ()
{
float square_area,side1,side2;
cout << "Input Side 1: "; cin >> side1;
cout<<endl;
cout << "Input Side 2: "; cin >> side2;
cout<<endl;
square_area = side1 * side2;
cout << "The area of the square is: " << square_area << endl;
}