If statements and functions

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  
#include <iostream> 
#include <cmath>
using namespace 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;		
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
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
}
else if (type=='s'){
   //code for the square
}
else
   cout << "invalid input\n";


1
2
float tri_area(float, float);
float squ_area(float, float);
use the parameters and the return value, don't make input/output operations inside those functions
closed account (1vf9z8AR)
here.For some reason area of triangle always comes to be zero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace 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();
        else if(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;
}

Topic archived. No new replies allowed.