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

AI Thread 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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top