Below is my problem...


Here is my code...

#include<iostream>
#include<iomanip> //allows me to use setflags/showpoint and set precision
#include <stdlib.h>	//for exit(1)
#include <fstream>
# include <stdlib.h>
# include <cstdlib>
using namespace std;
int main()

{
double s1, s2, s3;
    ifstream inf; 
    inf.open ("C:\\CS201HWASSGN6INPUTFILE.txt", ios::in);
        ifstream infile("C:\\CS201HWASSGN6INPUTFILE.txt", ios::in); 
		//Input file open and use
		if(!infile)
				{
				cerr<<"Input file could not be opened"<<endl;
					// Error Trap - end program if input file does not open
					exit(1);
				}
        ofstream outfile("c:\\CS201HWASSGN6OUTPUTFILE.txt", ios::out);
		//Output file 
		
		if ( ! outfile ) 
				{
				cerr << "Output file could not be opened" << endl;
				//Error Trap - end program if output file does not open
				exit(1);
				}

				outfile << setiosflags(ios::fixed | ios::showpoint);
				outfile << setprecision(3);

				cout<<setiosflags(ios::fixed |ios::showpoint);
				cout<<setprecision(3); 
				//set precision 3 means the
				//output will show 3 places after the dpoint 18.000


				infile>>s1; s2; s3;

			{cout<<setw(25)<<"Sides: "<< setw(35) 
				<<"Triangle? "<< setw(40)
				<<"Perimeter: "<<setw(45) 
				<<"Area "<< endl<< endl;
			cout<<setw(10) <<s1<< s2<<s3<<endl;}
			{outfile <<setw(10)<<"Sides: "<< setw(20) 
				<<"Triangle? "<< setw(30)
				<<"Perimeter: "<<setw(40) 
				<<"Area "<< endl<< endl;
			outfile<<setw(10) <<s1<< s2<<s3<<endl;}
			
   system("\t\t\tPAUSE"); //this allows my screen to pause
infile.close();
outfile.close();

return 0;
}

Write a C++ program which will read an undetermined number of real number triplets from an external data file. These triplets represent the 3 possible sides of a triangle.

Your program will:

		1.  Determine if the sides do in fact form a triangle. 
			** The sum of any two sides must be greater
			      than the third side 

		2.  If the triangle is possible, then calculate the perimeter correct to 3
		    decimal places.
			** perim = a + b + c

		3.  If the triangle is possible then calculate the area, correct to 3 decimal
		     places, using Heron's formula:
			** area = sqrt(s * (s - a) * (s - b) * (s - c))
		                      where s = (a + b + c) / 2

Include the <math.h> or <cmath> library in order to have access to the sqrt() function (which has an argument and return type of double).


The output should be in tabular form. For example:

Sides        	Triangle?      Perimeter      Area

		1.000 2.000 3.000       false          
		3.000 4.000 5.000       true              12.000             6.000

You must include four programmer defined functions whose prototypes are:

bool isATriangle(const double s1, const double s2, const double s3),
double semiPerimeter(const double s1, const double s2, const double s3)
double perimeter(const double s1, const double s2, const double s3)
double area(const double s1, const double s2, const double s3)

The function area() should call the function semiPerimeter() .The sides, perimeter and area should be correct to 3 decimal places. Please show the decimal values even
if they are zeros. Use the stream manipulators setw(), setprecision() and setiosflags() in order to align and guarantee the correct number of decimal places in your output and also use the manipulator boolalpha in order to print true/false in the Triangle? column.

You may include additional functions if you desire. You may want to include the
following code segment to control the while loop:

while (inf>>side1>>side2>>side3)
{ <body of loop> }

where inf is the programmer defined external input file object.

Create an external formatted data file containing the following data set:

3.0      4.0       5.0
				1.0      2.0       3.0
				2.0      2.0       3.9
				5.12    6.33  10.69
				3.45    2.91    6.77
				8.0    12.0     13.0
				2.9      2.9       2.9
				9       10          2

Not

infile>>s1; s2; s3;

But

infile>>s1>>s2>>s3;

After that, check your program for bugs and return if you need any help. And yes, use code tags for posting your code from now onwards.

Not

But

infile>>s1>>s2>>s3;

After that, check your program for bugs and return if you need any help. And yes, use code tags for posting your code from now onwards.

Hi Jishnu!!!
Thanks!!!
I did not understand code tags, but I got it now :)
This is what I have now...

However, I need to do this using functions, which I am totally lost... Any help would be greatly appreciated.

would like help doing this using functions, and to check if my logic is correct are all of these triangles? And my values for area are not correct, I know I did something wrong...

Anyway... Heres my code

#include<iostream>
#include<iomanip> //allows me to use setflags/showpoint and set precision
#include <stdlib.h>	//for exit(1)
#include <fstream>
# include <stdlib.h>
# include <cstdlib>
# include <cmath>
using namespace std;

//function areYOUtrianle prototype
//void areYOUtriangle(double, double, double);
//double areYOUtriangle(double, double, double);
//bool isATriangle (const double s1, const double s2, const double s3);
//double semiPerimeter (const double s1, const double s2, const double s3);
//double perimeter (const double s1, const double s2, const double s3);
//double area (const double s1, const double s2, const double s3);

int main()

{
ifstream inf; 
inf.open ("C:\\CS201HWASSGN6INPUTFILE.txt", ios::in);
ifstream infile("C:\\CS201HWASSGN6INPUTFILE.txt", ios::in); 
//Input file open and use
if(!infile)
{
cerr<<"Input file could not be opened"<<endl;
// Error Trap - end program if input file does not open
exit(1);
}
ofstream outfile("c:\\CS201HWASSGN6OUTPUTFILE.txt", ios::out);
//Output file 

if ( ! outfile ) 
{
cerr << "Output file could not be opened" << endl;
//Error Trap - end program if output file does not open
exit(1);
}
outfile << setiosflags(ios::fixed | ios::showpoint);
outfile << setprecision(3);
cout<<setiosflags(ios::fixed |ios::showpoint);
cout<<setprecision(3); 
//set precision 3 shows3decimal places

//declare varilebs in main
bool flag1 = true, flag2 = false;
double s1, s2, s3;
double perim;
double s;
s = (s1+s2+s3)/2;
double area;
int sides;

{
cout<<setw(15)	<<"Sides: "<<setw(30)<<"Triangle? "
				<<"\tPerimeter: "
				<<"\tArea: "<< endl<< endl;
//cout<<setw(6)<<s1<<setw(8)<<s2<<setw(10)<<s3;
}
//cout<<endl<<endl;

{
outfile <<setw(15)	<<"Sides: "<<setw(30)<<"Triangle? "
					<<"\tPerimeter: "
					<<"\tArea: "<< endl<< endl;
//outfile<<setw(6)<<s1<<setw(8)<<s2<<setw(10)<<s3<<endl;
}
outfile<<endl<< endl;


while (infile>>s1>>s2>>s3){	
		//infile>>s1>>s2>>s3;
		cout<<setw(6)<<s1<<setw(8)<<s2<<setw(10)<<s3;
			{if
				(s1+s2>s3&&s1+s3>s2||s2+s3>s1)
				cout << setw(15)<<boolalpha<<flag1;
				else if 
				(s1+s2<s3||s1+s3<s2||s2+s3<s1)
				cout << setw(15)<<boolalpha<<flag2;
				}
if (flag1 =1)
perim = s1 + s2+ s3;
cout<<setw(15)<< perim;
if (flag1 = 1)
area = sqrt(s-s1) * (s-s2) * (s-s3);
cout << setw(15)<<area;
cout<<endl<< endl<<endl;

}//end while loop
cout<<endl<< endl<<endl;

   system("\t\t\tPAUSE"); //this allows my screen to pause
infile.close();
outfile.close();

return 0;
}

However, I need to do this using functions, which I am totally lost... Any help would be greatly appreciated.

would like help doing this using functions, and to check if my logic is correct are all of these triangles? And my values for area are not correct, I know I did something wrong...

Instead of

area = sqrt(s-s1) * (s-s2) * (s-s3);

use

area = sqrt( s * (s-s1) * (s-s2) * (s-s3) );

That would give you the correct value of the areas. Try building the same with functions if you want to do so. Post any further problems you encounter.

Hi Thanks again,

My final problem is I do not know how to do this with functions.

Can you lead me in the right direction?

Hi Thanks again,

My final problem is I do not know how to do this with functions.

Can you lead me in the right direction?

Well, the first required function is the bool isATriangle(const double s1, const double s2, const double s3); With regard to that, you could have something like ...

// a function prototype
bool isATriangle(const double s1, const double s2, const double s3);

int main()
{
    double s1, s2, s3;

    // code here that gets the values for s1..s3

    bool bTriangle = isATriangle(s1, s2, s3);

    if(bTriangle)
    {
        // it is a triangle
    }
    else
    {
        // it is NOT a triangle
    }

    return 0;
}

// function definition
bool isATriangle(const double s1, const double s2, const double s3)
{
    // three values are passed in ...

    if( /* add code to check here whether it is a triangle */  )
    {
        // it is a triangle
        return true;
    }
    else
    {
        // it is NOT a triangle
        return false;
    }
}

Well, the first required function is the bool isATriangle(const double s1, const double s2, const double s3); With regard to that, you could have something like ...

// a function prototype
bool isATriangle(const double s1, const double s2, const double s3);

int main()
{
    double s1, s2, s3;

    // code here that gets the values for s1..s3

    bool bTriangle = isATriangle(s1, s2, s3);

    if(bTriangle)
    {
        // it is a triangle
    }
    else
    {
        // it is NOT a triangle
    }

    return 0;
}

// function definition
bool isATriangle(const double s1, const double s2, const double s3)
{
    // three values are passed in ...

    if( /* add code to check here whether it is a triangle */  )
    {
        // it is a triangle
        return true;
    }
    else
    {
        // it is NOT a triangle
        return false;
    }
}

Hi

Thank you for your help!

I've got this almost work - I now just need help with the last part, the function for Area, could you take a look?

here's my code:

#include<iostream>
#include<iomanip> //allows me to use setflags/showpoint and set precision
#include <stdlib.h>	//for exit(1)
#include <fstream>
# include <stdlib.h>
# include <cstdlib>
# include <cmath>
using namespace std;

//function areYOUtrianle prototype
//void areYOUtriangle(double, double, double);
//double areYOUtriangle(double, double, double);
bool isATriangle (const double s1, const double s2, const double s3);
double semiPerimeter (const double s1, const double s2, const double s3);
double perimeter (const double s1, const double s2, const double s3);
double area (const double s1, const double s2, const double s3);

int main()

{
ifstream inf; 
inf.open ("C:\\CS201HWASSGN6INPUTFILE.txt", ios::in);
ifstream infile("C:\\CS201HWASSGN6INPUTFILE.txt", ios::in); 
//Input file open and use
if(!infile)
{
cerr<<"Input file could not be opened"<<endl;
// Error Trap - end program if input file does not open
exit(1);
}
ofstream outfile("c:\\CS201HWASSGN6OUTPUTFILE.txt", ios::out);
//Output file 

if ( ! outfile ) 
{
cerr << "Output file could not be opened" << endl;
//Error Trap - end program if output file does not open
exit(1);
}
outfile << setiosflags(ios::fixed | ios::showpoint);
outfile << setprecision(3);
cout<<setiosflags(ios::fixed |ios::showpoint);
cout<<setprecision(3); 
//set precision 3 shows3decimal places

//declare varilebs in main
bool flag1 = true, flag2 = false;
double s1, s2, s3;
double perim;
//double s;
//s = (s1+s2+s3)/2;
double Area;
//int sides;

{
cout<<setw(15)	<<"Sides: "<<setw(30)<<"Triangle? "
				<<"\tPerimeter: "
				<<"\tArea: "<< endl<< endl;
//cout<<setw(6)<<s1<<setw(8)<<s2<<setw(10)<<s3;
}
//cout<<endl<<endl;

{
outfile <<setw(15)	<<"Sides: "<<setw(30)<<"Triangle? "
					<<"\tPerimeter: "
					<<"\tArea: "<< endl<< endl;
//outfile<<setw(6)<<s1<<setw(8)<<s2<<setw(10)<<s3<<endl;
}
outfile<<endl<< endl;


while (infile>>s1>>s2>>s3)
{	
		cout<<setw(6)<<s1<<setw(8)<<s2<<setw(10)<<s3;
		
			if(isATriangle(s1, s2, s3) ==true)
		{
		 	cout << setw(15) << "true";
			perim = perimeter(s1, s2, s3);
			cout<<setw(16)<< perim;


//			area = area(s1, s2, s3);
			cout<<setw(14)<<Area;
			cout<<endl;
		}
		else
			cout << setw(16) << "false"<<endl;
		 	cout<<endl;
		 	
		//	{if
			//	(s1+s2>s3&&s1+s3>s2||s2+s3>s1)
				//cout << setw(15)<<boolalpha<<flag1;
				//else if 
				//(s1+s2<s3||s1+s3<s2||s2+s3<s1)
				//cout << setw(15)<<boolalpha<<flag2;
				//}
//if (flag1 =1)
//perim = s1 + s2+ s3;
//		perim = perimeter(s1, s2, s3);
//		cout<<setw(15)<< perim;
//if (flag1 = 1)
//		Area = area(s1, s2, s3);
//		cout<<setw(15)<<Area;
//		cout<<endl<<endl<<endl;
//area = sqrt(s *(s-s1) * (s-s2) * (s-s3));
//cout << setw(15)<<area;
//cout<<endl<< endl<<endl;

}//end while loop
cout<<endl<< endl<<endl;

   system("\t\t\tPAUSE"); //this allows my screen to pause
infile.close();
outfile.close();

return 0;
}
//double perim;
bool isATriangle (const double s1, const double s2, const double s3)
{
 	 return (s1+s2>s3 && s2+s3>s1 && s1+s3>s2);
}	 
double semiPerimeter (const double s1, const double s2, const double s3)
{
 	   return perimeter(s1, s2, s3)/2;
}
double perimeter (const double s1, const double s2, const double s3)
{
 	   return s1+s2+s3;
}

//double area (const double s1, const double s2, const double s3)
//{
//return area = area(s1, s2, s3);
//}

Use these statements inside the area function.

area = sqrt( s * (s-s1) * (s-s2) * (s-s3) );
return area;
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.