- #1
PleaseHelp
- 1
- 0
I have been given a project to modify my pre-existing code to satisfy these guidelines:
- 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.
I have had some suffered some difficulties understanding the directions and have, therefore, been unable to complete this project. Some help would be appreciated!// This is my code currently
- 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.
I have had some suffered some difficulties understanding the directions and have, therefore, been unable to complete this project. Some help would be appreciated!// This is my code currently
Code:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
/*** Triangle class definition ***/
class Triangle {
public:
void GetSides ();
double GetArea ();
private:
double side1 = 0.0;
double side2 = 0.0;
double side3 = 0.0;
};
void Triangle::GetSides(){
double sideOne = 0.0;
double sideTwo = 0.0;
double sideThree = 0.0;
cout << "Please enter three sides of a triangle." << endl;
cin >> sideOne >> sideTwo >> sideThree; while ((sideOne<=0) || (sideTwo<=0) || (sideThree<=0) || (sideOne+sideTwo > sideThree) || (sideOne+sideThree > sideTwo) || (sideTwo+sideThree > sideOne)){
cout << "! ERROR !"<< endl;
cout << "Using sides { " << sideOne << " " << sideTwo << " " << sideThree << " } will NOT make a triangle." << endl;
cout << "Please re-enter three sides of the triangle." << endl;
cin >> sideOne >> sideTwo >> sideThree;
}
cout << endl;
cout << "Using sides { " << sideOne << " " << sideTwo << " " << sideThree << " } will make a triangle." << endl;
side1 = sideOne;
side2 = sideTwo;
side3 = sideThree;
return;
}double Triangle:: GetArea(){
double p = 0.0;
double areaTriangle = 0.0;
double a = 0.0;
double b = 0.0;
double c = 0.0;
a = side1;
b = side2;
c = side3;
p = (a + b + c) / 2;
areaTriangle = sqrt( p*(p - a)*(p - b)*(p - c) );
return areaTriangle ;
}
/*** End definitions for Triangle class ***/
/*** Functions for vector of Triangle objects ***/void tPrintVector (vector<Triangle>& vTriangle){
cout << "Printing area of triangle(s). " << endl;
for (int i = 0; i < vTriangle.size(); ++i){
cout << "Triangle " << i+1 << " has an area of " << vTriangle.at(i).GetArea() << endl;
}
return;
}
/*** End functions for vector of Triangle objects ***/
int main() {
char userInput = 'c';
vector<Triangle> triangles;
Triangle temp;
// cout << endl << "Press ( c ) to continue, ( q ) to quit." << endl;
while (userInput != 'q'){
temp.GetSides();
triangles.push_back(temp);
tPrintVector(triangles);
cout << endl << "Press ( c ) to continue, ( q ) to quit." << endl;
cin >> userInput;
cout << endl;
}
// tPrintVector(triangles);
cout << "Done." << endl;
return 0;
}
Last edited by a moderator: