Functions driving me crazy

Hey guys, iam assigned a program that has to read in 3 values (sides of triangles) at a time and find the area. The values have to be read from a file, they are in a loop as there can be as many sets of triangles as the user wishes, the area needs to be found and the results are output into a file. Oh, most of the features in the program need to be done through functions.

I have a program that compiles fine but it does not work. Can you find any mistake in this? :

Input (prog6_inp.txt): 10.2 6.5 8.1
10.0 10.0 10.0
-1.0 0.0 6.0

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <fstream> 
#include <math.h> 
#include <iomanip>

using namespace std;


void printIdinfo(ofstream &);
void readData(ifstream &, double, double, double);
void isEquilateral(ofstream &);
double areaTriangle(double, double, double);
double areaEquilateral(double, double, double);
double perimeterTriangle(double, double, double);
void printResults(ofstream &, double, double, double, double, double);
void printOutputFilename();

void printIdinfo(ofstream &fout)
{
    fout << "XXXXXXX" << endl 
         << "XXXXXXXX" << endl 
         << "XXXXXXXX" << endl << endl << endl;
}

void readData(ifstream &fin, double sidea, double sideb, double sidec)
{
     fin >> sidea >> sideb >> sidec;
}

double perimeterTriangle (double sidea, double sideb, double sidec)
{
    double result;
    result = sidea + sideb + sidec;
    return result;
}

void isEquilateral(ofstream &fout)
{
     cout << endl << "The triangle is equilateral." 
          << endl;
}

double areaEquilateral(double sidea, double sideb, double sidec)
{
       double area;
       area = ( pow ( sidea, 2 ) * sqrt ( 3 ) ) / 4.0;
       return area;
}
       
double areaTriangle(double sidea, double sideb, double sidec, double perimeter)
{
       double area;
       double s;
       s = perimeter / 2.0;
       area = sqrt (s * (s - sidea) * (s - sideb) *  (s - sidec));
       return area;
}

void printResults(ofstream &fout, double sidea, double sideb, double sidec, double area,
                  double perimeter)
{
     fout << setprecision(1) << fixed;
     fout << "The sides of the triangle are " << sidea << ", " 
          << sideb << " and " << sidec << "." << endl 
          << "The perimeter of the triangle is " << perimeter 
          << "." << endl << "The area of the triangle is " << area
          << "." << endl << endl;  
}

void printOutputFilename()
{
      cout << "The results of the calculations can be found in prog6_out.txt" 
        << endl << endl;
}
     
int main() 
{
    double sidea,
           sideb,
           sidec,
           perimeter,
           area;
           
    ifstream fin;
    ofstream fout;
    fin.open("prog6_inp.txt");
    fout.open("prog6_out.txt");
    
     if ( !fin )
    { 
         cout << "The input file cannot be opened. Program terminated." 
              << endl << endl;
         system ("PAUSE");
         return 1;
    }
    
     if ( !fout )
    { 
         cout << "The output file cannot be opened. Program terminated." 
              << endl << endl;
         system ("PAUSE");
         return 2;
    }
     
    printIdinfo(fout);
    
  while ( !fin.eof()) 
  {
          
   readData(fin, sidea, sideb, sidec);
       
     if ( sidea == sideb && sideb == sidec ) 
        {     
            perimeter = perimeterTriangle (sidea, sideb, sidec);
            area = areaEquilateral(sidea, sideb, sidec);
            isEquilateral(fout);
            printResults(fout, sidea, sideb, sidec, area, perimeter);
        }
       
     else 
         {
            perimeter = perimeterTriangle (sidea, sideb, sidec);
            area = areaTriangle(sidea, sideb, sidec, perimeter);
            
            printResults(fout, sidea, sideb, sidec, area, perimeter);
                 
         }  
   }
       
             
   printOutputFilename();
    
   fin.close();
   fout.close();
   
   system("PAUSE");
   return 0;
    
}
1
2
//void readData(ifstream &, double, double, double);
  void readData(ifstream &, double &, double &, double &);

You also misspell a fout

Some comments:

double areaTriangle(double sidea, double sideb, double sidec, double perimeter)
Instead of passing perimeter as a param, just calculate it in the function.

double areaEquilateral(double sidea, double sideb, double sidec)
You don't use sideb or sidec, why need to put them?
Topic archived. No new replies allowed.