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

In summary, this conversation discusses modifying pre-existing code to meet certain guidelines. The guidelines include creating a file called "sides.txt" and implementing a constructor and a global function to validate and save side values for a triangle. The conversation also mentions the importance of understanding file input/output and provides a possible solution for the project.
  • #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
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
  • #2
uuum...i have that exact same project from my instructor. with the exact same guidelines. (Speechless)
 
  • #3
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:

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

How do I add a stream to my pre-existing C++ code?

To add a stream to your pre-existing C++ code, you will need to include the <iostream> library in your code. This will allow you to use the cin and cout objects to read input from the user and display output, respectively.

What is the purpose of adding streams to my code?

The purpose of adding streams to your code is to allow for input and output operations within your program. This can be useful for interacting with the user, displaying information, and reading data from external sources.

Can I add multiple streams to my code?

Yes, you can add multiple streams to your code by declaring and initializing multiple cin and cout objects. You can also use other types of streams, such as ifstream and ofstream, to read from and write to files.

How do I handle errors when using streams?

To handle errors when using streams, you can use the fail and clear functions to check for and clear any errors that may occur during input or output operations. You can also use try-catch blocks to catch and handle specific errors.

Is it possible to remove streams from my code?

Yes, it is possible to remove streams from your code by removing the corresponding #include statement and any code that uses the streams. However, it is generally recommended to keep streams in your code for future use or for maintainability purposes.

Similar threads

Replies
5
Views
2K
Replies
23
Views
2K
Replies
6
Views
10K
Replies
14
Views
32K
Replies
1
Views
3K
Replies
35
Views
3K
Back
Top