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
#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;
}