#include #include #include using namespace std; class Triangle{ public: double a
ID: 3570966 • Letter: #
Question
#include
#include
#include
using namespace std;
class Triangle{
public:
double a;
double b;
double c;
void GetSides(){
do{
cout << "Enter the three side: "< cin >> a >> b >> c;
if (!((a > 0) && (b > 0) && (c > 0) && ((a + b) > c) && ((a + c) > b) && ((b + c) > a)))
{
cout << "Error!"< }
} while (!((a > 0) && (b > 0) && (c > 0) && ((a + b) > c) && ((a + c) > b) && ((b + c) > a)));
}
double GetArea(){
double s, A;
s = (a + b + c)/2;
A = sqrt(s*(s - a)*(s - b)*(s - c));
return A;
}
};
int main()
{
vector triangle;
double area;
char answer = 'y';
do{
Triangle temp;
temp.GetSides();
triangle.push_back(temp);
cout << "Do you want to continue? : ";
cin >> answer;
}while (answer == 'y');
for(int i = 0; i < triangle.size(); i++){
area = triangle[i].GetArea();
cout << "Area of Triangle " << i+1 << " : " << area << endl;
}
return 0;
}
i need to edit the code above to do:
create a file called "sides.txt" and put it in the same folder as your program. The file contains multiple lines with each line having the three sides, separated by white spaces, e.g.,
3 4 5
6 8 10
5 6 7
....
declare and define a constructor that gets the sides of a triangle from the valid side values read from the file. Not here that you need to create a function (similar to what's in getSides() in project 3) to validate the sides before calling the constructor;
declare and define a global function saveArea() that saves all area values to a file called "areas.txt", located in the same folder.
It's not required but you are encouraged to use "new" to create pointers to Triangle objects and "delete" to release memory. Then your vector is a vector of Triangle pointers.
Your sides.txt file should contain at least 5 lines.
AND THE CODE MUST INCLUDE MY CONDITIONS IN THE IF STATEMENT AND THE WHILE LOOP. and please run the code before you post.
Explanation / Answer
Program code:
// Area of Triangle.cpp: Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<fstream>
#include<cmath>
using namespace std;
//class Triangle which contains parameterised
//constructor and GetArea method
class Triangle
{
public:
double a, b, c;
Triangle(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
double GetArea()
{
double s, A;
s = (a + b + c)/2;
A = sqrt(s*(s - a)*(s - b)*(s - c));
return A;
}
};
//prototypes
void GetSides(vector<Triangle> &triangle);
void saveArea(vector<Triangle> &triangle);
//main function
int main()
{
//create vector objects of triangles
vector<Triangle> triangle;
//call GetSides function
GetSides(triangle);
//call saveArea function
saveArea(triangle);
system("pause");
return 0;
}
//GetSides funciton reads the data from the file, verifies the values
//then creates an object and then stores the triangle objects into
//vectors
void GetSides(vector<Triangle> &triangle)
{
int a, b, c;
ifstream readfile("sides.txt");
if(readfile)
{
//read the data from the file
while(readfile!=NULL)
{
readfile>>a;
readfile>>b;
readfile>>c;
//validate the inputs
if (!((a > 0) && (b > 0) && (c > 0) && ((a + b) > c) && ((a + c) > b) && ((b + c) > a)))
{
cout << "Error!"<<endl<<"Re-";
}
else
{
//create an object to the Triangle class
Triangle t = Triangle(a, b, c);
//place the objects in the vector
triangle.push_back(t);
}
}
}
//close the file
readfile.close();
}
//saveArea function that takes the reference to the vector
void saveArea(vector<Triangle> &triangle)
{
double area;
ofstream writeFile("areas.txt");
if(writeFile)
{
//write the data into the file
for(int i = 0; i < triangle.size()-1; i++)
{
area = triangle[i].GetArea();
writeFile<<triangle[i].a<<" "<<triangle[i].b<<" "<<triangle[i].c<<" "<<" "<<area<<endl;
}
}
writeFile.close();
}
---
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.