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;
}
C++ program: area of triangle/rectangle
Page 1 of 14 Replies - 8338 Views - Last Post: 21 October 2009 - 09:49 PM
Replies To: C++ program: area of triangle/rectangle
#2
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
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
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
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
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;
}
#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
Re: C++ program: area of triangle/rectangle
Posted 21 October 2009 - 09:49 PM
change char side to int side
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|