C/C++ C++ Programming Problem: Adding Streams to a Pre-Existing Code

Click For Summary
The discussion revolves around a programming project that requires modifying existing code to adhere to specific guidelines for handling triangle data. Key requirements include creating a "sides.txt" file containing multiple sets of triangle sides, implementing a constructor in a Triangle class to read and validate these sides, and defining a global function to save calculated triangle areas to "areas.txt". The original poster expresses confusion about the project requirements and seeks assistance. Several participants provide insights into file input/output operations, emphasizing the importance of correctly opening and closing files. They suggest using pointers for Triangle objects to manage memory effectively and offer code snippets demonstrating how to implement the required functionality, including reading from the "sides.txt" file and validating triangle sides. A focus is placed on the need for a function to check if the sides form a valid triangle and the importance of avoiding global variables for better programming practice. Overall, the discussion highlights the challenges of understanding project specifications and the collaborative effort to clarify and solve coding issues.
PleaseHelp
Messages
1
Reaction score
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
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:
Technology news on Phys.org
uuum...i have that exact same project from my instructor. with the exact same guidelines. (Speechless)
 
Hi,
You must understand a little about file I/O. In particular, you need to know about opening and closing a file. Here's a lot of a solution. I have left the saveArea function to you. This program has the function:
void saveAreas(vector<Triangle*> v);
Slightly different from the specification. If you don't supply the vector as parameter, you must make the vector of pointers to triangles a global variable. I try and avoid global variables -- this is good programming practice.

Code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>

using namespace std;

class Triangle {
  int side1, side2, side3;
public:
  Triangle(int s1, int s2, int s3);
  double getArea();
  int getSide(int which);
};

// It is the responsibility of the caller to ensure the triangle is valid.
Triangle::Triangle(int s1, int s2, int s3) {
  side1 = s1;
  side2 = s2;
  side3 = s3;
}

// Computes and returns the area by Heron's formula
double Triangle::getArea() {
  double p = (side1 + side2 + side3) / 2.0;
  double a = sqrt(p * (p - side1)*(p - side2)*(p - side3));
  return (a);
}

// Returns a side of the triangle.  Since sides are private, need such a fn.
int Triangle::getSide(int which) {
  if (which == 1) {
    return (side1);
  }
  if (which == 2) {
    return (side2);
  }
  return (side3);
}

int isTriangle(int s1, int s2, int s3);
void saveAreas(vector<Triangle*> v);

int main(int argc, char** argv) {
  vector<Triangle*> vec;
  int s1, s2, s3;
  ifstream inFile("sides.txt");
  if (inFile.is_open()) {
    while (inFile >> s1 >> s2 >> s3) {
      if (isTriangle(s1, s2, s3)) {
        vec.push_back(new Triangle(s1, s2, s3));
      } else {
        cout << "Input file corrupted" << endl;
      }
    }
    inFile.close();
    saveAreas(vec);
  } else {
    cout << "could not open input file" << endl;
  }
  return 0;
}

// returns true (1) if and only if s1, s2 and s3 are sides of valid triangle
int isTriangle(int s1, int s2, int s3) {
  return (s1 > 0 && s2 > 0 && s3>0 && s1 + s2 > s3 && s1 + s3 > s2 && s2 + s3 > s1);
}

// outputs all triangles *v[i] in vector v to file "areas.txt"
// after each triangle is output, it is delete
void saveAreas(vector<Triangle*>v) {
  
}
 
Last edited:
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
12
Views
2K
Replies
10
Views
2K