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

Recommended Answers

All 3 Replies

What do you mean by "does not work"? What is an example triple of input? The current output? The expected output?

I'd recommend making a small demo program to test the functions without any user interaction - that is, hard code three values and see if it gives you the correct result. Separating the input state from the computation stage is always a good idea to at least make sure you're looking in the right place for the problem!

You need to modify function parameters to pass them by reference instead of by value so that the changes made by the function can be seen by the whoever called it. void readData(ifstream &fin, double &sidea, double &sideb, double &sidec) Now do something similar with the other functions.

Ancient Dragon, my deepest thanks!

I would have sat here all day and not realized that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.