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

Click For Summary
SUMMARY

The forum discussion centers on modifying a C++ program to read triangle side lengths from a file named "sides.txt" and calculate their areas. The user is required to implement a constructor in the Triangle class that initializes triangle sides and a global function, saveArea(), to write area values to "areas.txt". Key challenges include understanding file I/O operations and ensuring triangle validity through proper validation functions. The provided code snippets illustrate both the original and modified implementations, emphasizing best practices in memory management and object-oriented programming.

PREREQUISITES
  • C++ programming fundamentals
  • File I/O operations in C++
  • Object-oriented programming concepts
  • Understanding of Heron's formula for area calculation
NEXT STEPS
  • Implement file reading and writing in C++ using ifstream and ofstream
  • Learn about dynamic memory management in C++ with new and delete
  • Explore validation techniques for triangle side lengths
  • Study best practices for using vectors and pointers in C++
USEFUL FOR

C++ developers, computer science students, and anyone looking to enhance their skills in file handling and object-oriented programming in C++.

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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K