#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "hw9_V1.h" // Triangle class
usingnamespace std;
void openInAndOutFiles();
int main() {
Triangle triangle1;
ifstream fin;
ofstream fout;
openInAndOutFiles();
for (int i = 0; !fin.eof(); i++) {
double side;
fin >> side;
// Debug:
cout << "side: " << side << endl;
}
return 0;
}
void openInAndOutFiles() {
// Create file objects:
ifstream fin;
ofstream fout; // Needed to check if it has opened properly.
// Create file name variables:
string inFileName, outFileName;
// Ask user to enter an input file name:
cout << "Please type your input file name: ";
cin >> inFileName;
//string inFileName = "input.txt"; // Use this while testing.
// Ask user to enter an output file name:
cout << "Please type your output file name: ";
cin >> outFileName;
//string outFileName = "output.txt"; // Use this while testing.
// Open Input & Output files.
fin.open(inFileName);
fin.open(outFileName, fstream::app);
// Check to make sure the input file is actually open:
if (fin.fail()) {
cerr << "Error opening input file."; // cerr is used for error messages. Learned about in https://youtu.be/Iho2EdJgusQ?t=353
exit(1);
}
// Check to make sure the output file correctly opens:
if (!fout.is_open()) {
cerr << "Output file " << outFileName << " couldn't be opened to write to.." << endl;
}
}
class Triangle {
private: // Only functions INSIDE the class can access this.
double side1, side2, side3; // declaring the sides of the triangle
double perimeter;
double area;
string type;
public: // Functions outside of the class can access this.
// Define a method (member function) that will calculate and return the perimeter of a triangle.
double getPerimeter(double s1, double s2, double s3) {
double peri;
return peri = s1 + s2 + s3;
}
// Define a method that will calc and return the area of a triangle. Hint: Use Heron's formula:
// http://www.mathsisfun.com/geometry/herons-formula.htmldouble getArea(double s1, double s2, double s3) {
double area;
double s;
s = (s1 + s2 + s3) / 2;
area = sqrt( s*(s - s1)*(s - s2)*(s - s3) );
return area;
}
// Define a method that will find and return whether the triangle is Equilateral, Isosceles, or Scalene.
string getType(double s1, double s2, double s3) { // all sides equal each other.
if (s1 == s2 && s1 == s3) {
return type = "Equilateral";
}
elseif (s1 == s2 || s1 == s3 || s2 == s3) { // at least 2 sides must equal each other.
return type = "Isosceles";
}
elseif (s1 != s2 && s1 != s3 && s2 != s3) { // no sides equal each other.
return type = "Scalene";
}
}
// Define a method that will find and return the largest AND smallest sides of a triangle.
// Same idea as max & min.
double getLargestOrSmallest(double s1, double s2, double s3) {
double sides[3] = { s1, s2, s3 };
double largest;
double smallest;
for (int i = 0; i < 3; i++) {
largest = sides[0];
if (sides[i] > largest) largest = sides[i];
if (sides[i] < smallest) smallest = sides[i];
}
return largest, smallest;
}
};
@Thomas1965
Ah okay, didn't realize that was required. My professor said a header file is basically just combined by the compiler into the main file where the #include "example.h" line is.
your professor is correct, but you are not understanding.
you can, for example, do this in a text file:
cout << smallest;
and nothing else.
now type #include "yourfilename" on say line 45. what happens? (well, smallest is uninitialized, maybe give it a value of like 123.456 to be safe THEN try this experiment).
So, this shows what your professor was saying.
I would suspect that, the way you wrote it, the includes in main would be seen in the header because they came first. This is bad practice to rely on; it leads to massive problems on larger programs, but my first instinct is to suspect that
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "hw9_V1.h" // Triangle class //does not have namespace std:: access using namespace std; //and I don't think it would fix it to pull the header in after this statement, not 100% sure what happens to using statements in this scenario.
which leads to the "using namespace std causes problems" speech (see all the posts in the forum on this) but you could try putting that line in your header to see if that was the problem, and you can try putting your header after the using to see if it clears up if you want to experiment a bit.
after playing around with it, you can go for doing it the right way, which would be avoiding the using statement and putting the language headers in each file that uses them according to what was used in that file locally (this lets the files stand alone better, rather than require that somethingunrelated.h be included to use you generic whatever() function) and string together the headers that you wrote very carefully (including where needed and trying to not over-include them which creates unnecessary dependency). The larger your code becomes, the more absolutely critical getting your includes organized becomes. You may not even see a big issue with doing them fairly randomly during your whole school career, but on the job, it will be a hot topic if you scramble them!