area of triangle

So here is my question:

The area of an arbitrary triangle can be computed using the formula

Area = sqrt( s * (s-a) * (s-b) * (s-c) )

where a, b, and c are the lengths of the sides, and s is the semiperimeter:

s = (a+b+c) / 2

Write a void function that uses five parameters: three value parameters that
provide the length of the edges and two reference parameters that compute the
area and perimeter (not the semiperimeter). Make your function robust. Note
that not all combinations of a, b, and c produce a triangle. Have your program
read an input file named 'triangle.txt' which, on each line, contains the side
lengths. A sample file is available on Blackboard. You can assume that the
sides are all integers (and semiperimeter and area are doubles). Your output
should be similar to the following:

Output:
a=3, b=5, c=7, s=7.5, A=6.49519
a=5, b=7, c=9, s=10.5, A=17.4123
a=10, b=7, c=8, s=12.5, A=27.8107
a=9, b=7, c=3, s=9.5, A=8.78564
a=5, b=8, c=5, s=9, A=12
a=7, b=3, c=5, s=7.5, A=6.49519
a=4, b=6, c=8, s=9, A=11.619




The triangle.txt file is simply:
3 5 7
5 7 9
10 7 8
9 7 3
5 8 5
7 3 5
4 6 8

I am not sure what I am doing wrong but I think maybe my fstream or using a if statement is wrong. Any help is appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<cmath>
#include <fstream>

using namespace std;

int main() 
{
    double area,perimeter;
    int finish,a,b,c;
    bool compute_area(double&,double&,double,double,double);

    fstream inputFile;
    inputFile.open("triangle.txt");
    while (inputFile >> a >> b >> c){
        cout << "a=" << a << " b=" << b << " c=" << c << endl;


    bool valid = compute_area(area,perimeter,a,b,c);

    if (valid)
    {
        cout << "The area of a triangle with sides of length " << a
            << " , " << b << " and " << c << " is " << area <<
            "\nand the  perimeter is "<<perimeter<< endl;
    }


    cin>>finish;

    return 0;
}

bool compute_area (double& area, double& perimeter, double a, double b, double c) {
    double s;

    if( (a<(b+c))&&(b<(a+c))&&(c<(b+a)) )
    {
        s = (a + b + c)/ 2.0;
        area = sqrt (s*(s-a)*(s-b)*(s-c)); 
        perimeter=a+b+c;
    }
    else
    {
        cout<<"bad combination!"<<endl;

        return false;
    }
    return true;
}



I forgot to mention I was supposed to use void functions but I didn't in the code because I am not sure to implement them in the code.
Last edited on
Your loop at lines 15-16 is going to read the entire file.

When you reach line 19, a,b,c contain the values from the last record in the file.
You probably want to put lines 19-26 inside your while loop.

What is the error that you get?
@AbstractionAnon, I'm not sure what you mean, I thought it was in the while loop

@keskiverto, The error I get is:

In function 'int main()':
error: a function-definition is not allowed here before '{' token
error: expected '}' at end of input
There are mismatched opening and closing braces { }

At line 15
 
    while (inputFile >> a >> b >> c){

the opening left brace seems to be paired with the closing brace at line 32. But I don't think that's what was intended ?
Sorry now I get what AbstractionAnon and Chervil are saying. there was an extra '{' symbol on line 15. So the program runs and work but it only finds the area of the last line 4 6 8. How do I make find the area on every line? Thanks
How do I make find the area on every line?

Instead of considering the { at line 15 to be extra and unwanted, I think you need to put it back, and decide where is a suitable place to insert the matching closing brace.
Yes thank you Chervil, I figured it out. I'm not sure if my professor will mark me down because I didn't use void functions even though I was supposed to. I don't really know how to use them. Does anyone know how to convert this so it uses at least one void function? Thanks
Just a suggestion - there may be others.
In function compute_area()

You could change the return type of the function from bool to void
and instead of
cout<<"bad combination!"<<endl;
you might assign a value such as -1 to both area and perimeter.
and remove the return statements.

That way, the function does not return anything, it does not display anything, all communication is via the parameters.
You can assume that the sides are all integers (and semiperimeter and area are doubles).

Note that:
* You do read integers from file, but on calling the area-function you do convert the sides to doubles.
* While it makes sense to have the semiperimeter as double, nothing forces the perimeter to be double; a sum of three integers can be integer.
Topic archived. No new replies allowed.