Question

Please answer question in C++, thank you. Read in name of input file. File will be...

Please answer question in C++, thank you.

Read in name of input file. File will be formatted as below:

filename: triangles1.txt

3 4 5 7 7 7

For each line read in the 3 integer values as the 3 sides of a triangle and calculate the area and print on output line with two decimal points.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<cmath>


using namespace std;

/* main function begins*/
int main()
{
  
   char filename[30];
  
   cout<<"Enter the filename: ";
   cin>>filename;
  
   /* opening the file */
   ifstream infile(filename);
  
   //variables to store the perimeter
   double perimeter;
   double p;
  
  
   if(infile.fail())
   {
       cout<<"ERROR: File not found";
   }

   //variables to store the integers
   int a, b,c;
  
       //reading line by line
       while (infile >> a >> b>> c)
       {

           //calculating the perimeter
            perimeter=a+b+c;
          
          
            p=perimeter/2;
          
            //calculating the area using heron's formula
            double area=sqrt(p*(p-a)*(p-b)*(p-c));
          
            //displaying the details
            cout<<"side 1: "<<a<<endl;
            cout<<"side 2: "<<b<<endl;
            cout<<"side 3: "<<c<<endl;
            cout<<"Area : "<<area<<fixed<<setprecision(2)<<endl;  
            cout<<"\n"<<endl;
          
  
       }
}

CONTENTS OF triangles.txt:

3 4 5 7 7 7
12 5 8

SCREENSHOT FOR OUTPUT:

Enter the filename: triangles.txt side 1: 3 side 2: 4 side 3: !5 Area: 6 side 1:7 side 2:7 side 3:7 Area: 21.22 side 1: 12 s

Add a comment
Know the answer?
Add Answer to:
Please answer question in C++, thank you. Read in name of input file. File will be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT