• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ programing help

Soccerman06

Diamond Member
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.
 
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.
 
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"
 
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.
 
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.
 
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)
 
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.
 
Back
Top