4 Replies - 9885 Views - Last Post: 21 October 2009 - 09:49 PM Rate Topic: -----

#1 w3ndyb1rd  Icon User is offline

  • New D.I.C Head

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

C++ program: area of triangle/rectangle

Post icon  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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • 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 );
}




Was This Post Helpful? 1
  • +
  • -

#4 w3ndyb1rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

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

Posted 21 October 2009 - 09:49 PM

change char side to int side
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1