C++ programing help

Soccerman06

Diamond Member
Jul 29, 2004
5,830
5
81
Ok, I need help with the program I am writing, it calculates the area and perimeter of a triangle given sides a & b. Here is what I have so far (in C++):

# include <iostream>
using namespace std;

int main ()
{
float a;
float b;
float c;
float area;
float perimeter;

cout << "This program finds the perimeter and area of a triangle." << endl;
cout << "Enter the value for side a. ";
cin >> a;
if (a <= 0)
{
cout << "Bad Data.";
}

cout << "Enter the value for side b. ";
cin >> b;
if (b <= 0)
{
cout << "Bad Data.";
}

c = square((a*a)+(b*b));
perimeter = (a+b+c);
area = (1/2)*(a*b);


cout << "The perimeter of the triangle is " << perimeter;
cout << "The area of the triangle is: " << area;

system("pause");
return 0;
}

I know I need to add something like "# include <math>" or something like that so I can use the square root input.

What I need help with is that everytime I try to build the program, it doesnt find "c, or perimeter". What is the problem, and if there are any other problems with the code, what should I do. Please help, tooters are closed at 11 pm and no one in my dorm is in Comp Science and assignment is due tomorrow at 8 am.
 

natto fire

Diamond Member
Jan 4, 2000
7,117
10
76
Originally posted by: Soccerman06
Originally posted by: Captain_Howdy
Text

grrr I forgot about there.

No problem, ideally there would be a forum for just programming, so don't get too discouraged if your thread gets buried under the countless game and app threads.
 

Addis

Junior Member
Dec 29, 2004
15
0
0
While you're here, you'll need to include <math.h> first. then replace your non existent square function with sqrt (param).
I can compile it fine with dev-c++ when thats fixed.
 

reverend boltron

Senior member
Nov 18, 2004
945
0
76
yeah, and also, dont forget to throw in "#include <cstdlib>" for that system call. Depending on what compiler you're using. And also, format your output. try using:

cout << "The perimeter of the triangle is " << perimeter << ".\n";
cout << "The area of the triangle is: " << area << ".\n";

You can clean up your if statements too:
if (a <1 || b <1) cout << "Bad data"
 

sao123

Lifer
May 27, 2002
12,656
207
106
you should also include
if (a <1 || b <1) cout << "Bad data"
in a while loop so you dont try to compute with bad data. force them to keep inputting until all data is valid.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: sao123
you should also include
if (a <1 || b <1) cout << "Bad data"
in a while loop so you dont try to compute with bad data. force them to keep inputting until all data is valid.

.1 is a valid side width. The op is using floats not ints. I would recommand combining the ifs using OR but don't place them on the same line or leave out the { and } or you will lose style points.
 

sao123

Lifer
May 27, 2002
12,656
207
106
Originally posted by: smack Down
Originally posted by: sao123
you should also include
if (a <1 || b <1) cout << "Bad data"
in a while loop so you dont try to compute with bad data. force them to keep inputting until all data is valid.

.1 is a valid side width. The op is using floats not ints. I would recommand combining the ifs using OR but don't place them on the same line or leave out the { and } or you will lose style points.

It was a mis type. it should be
(a <0 || b <0)
 

Soccerman06

Diamond Member
Jul 29, 2004
5,830
5
81
Ok guys, the assignment was due monday, so its a tad late for any suggestions.

Anyways, I didnt post here because I made a thread in the software-app forum.