# C++ program: area of triangle/rectangle

Page 1 of 1

## 4 Replies - 11394 Views - Last Post: 21 October 2009 - 09:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=133548&amp;s=ac58960a365fd0f5ae06bbbd2a11a07c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 w3ndyb1rd

Reputation: 0
• Posts: 7
• Joined: 27-September 09

# C++ program: area of triangle/rectangle

Posted 21 October 2009 - 04:42 PM

For my assignment, I have to write a program that asks whether your shape is a triangle or rectangle, and then calculates the area for the shape you've identified.

This is the general idea of how we're supposed to write it, but I know I'm missing something. Or I just have things way out of order. Please help!

#include <iostream>
using namespace std;

void tri_area(float base, float height);
void rect_area(float base, float height);
void main(void)

{
char side;
cout << "How many sides does the shape have?\n";
cin >> side;

float length, width;
cout << "What is the length of the shape?\n";
cin >> length;
cout << "What is the width of the shape?\n";
cin >> width;
tri_area(length, width);
rect_area(length, width);
}

if (side= 3)
void tri_area(float base, float height)
else
void rect_area(float base, float height)

{
float area;
area= (base * height) / 2;
cout << "The area of the triangle is";
cout << area << endl;
}

{
float area;
area= base * height;
cout << "The area of the rectangle is";
cout << area << endl;
}

Is This A Good Question/Topic? 0

## Replies To: C++ program: area of triangle/rectangle

### #2 erik.price

• D.I.C Lover

Reputation: 485
• Posts: 2,690
• Joined: 18-December 08

## Re: C++ program: area of triangle/rectangle

Posted 21 October 2009 - 04:50 PM

First things I saw were, if (side= 3) this sets side EQUAL TO 3.

What you're looking for is this if (side == 3) this checks if side IS EQUIVALENT TO 3. Common error

void tri_area(float base, float height)
else
void rect_area(float base, float height)

the "void" implies you are declaring a function, get that out of there.

"float base" and "float height" will create new UNINITIALIZED variables. So, your compiler will either yell at you (if it didn't before) or return really funky values.

Is this something you wrote or something your teacher gave you that you needed to finish/correct?

Also, go back and edit your code so you use the [code][/ code] tags

This post has been edited by erik.price: 21 October 2009 - 04:52 PM

### #3 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: C++ program: area of triangle/rectangle

Posted 21 October 2009 - 04:52 PM

You need to define your functions? your compileer is not smart enough for you just to put random brackets
#include <iostream>

using namespace std;

float tri_area(float base, float height);
float rect_area(float base, float height);

int main()
{
int side = 0;
cout << "How many sides does the shape have?\n";
cin >> side;

float length, width;
cout << "What is the length of the shape?\n";
cin >> length;
cout << "What is the width of the shape?\n";
cin >> width;
if(side == 4)
cout<<"Area : "<<rect_area(length, width);
else if(side == 3)
cout<<"Area : "<<tri_area(length, width);
else
cout<<"ERROR"<<endl;

//pause window
cin.ignore();
cin.get();

return 0;
}
float tri_area(float base, float height)
{
return ((base*height)/2);
}
float rect_area(float base, float height)
{
return ( base * height );
}

### #4 w3ndyb1rd

Reputation: 0
• Posts: 7
• Joined: 27-September 09

## Re: C++ program: area of triangle/rectangle

Posted 21 October 2009 - 04:58 PM

Okay, I've fixed it up to this point, but when it runs it doesn't properly calculate the area when I say the shape has 3 sides. For some reason, the if statement isn't working.

#include <iostream>
using namespace std;

void tri_area(float base, float height);
void rect_area(float base, float height);
void main(void)

{
char side;
cout << "How many sides does the shape have?\n";
cin >> side;

float length, width;
cout << "What is the length of the shape?\n";
cin >> length;
cout << "What is the width of the shape?\n";
cin >> width;

if (side == 3)
tri_area(length, width);
else
rect_area(length, width);
}
void tri_area(float base, float height)
{
float area;
area= (base * height) / 2;
cout << "The area of the triangle is";
cout << area << endl;
}

void rect_area(float base, float height)
{
float area;
area= base * height;
cout << "The area of the rectangle is";
cout << area << endl;
}

### #5 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: C++ program: area of triangle/rectangle

Posted 21 October 2009 - 09:49 PM

change char side to int side