Help with output file

Hello! I need to output my code to a separate file but I'm not sure how to do so. This is what I am supposed to do:

Call all the functions defined to generate a output report file with proper formatting using iomanip library rounding floating point numbers to 2 decimal values

And here is my code that needs to be outputed into a file, text or data.

[code]
//Jordon Taylor - Triangles//
#include <iostream>
using namespace std;

void myFunction(float, float, float);
float perimeter(float, float, float);
float area(float, float, float);
float type(float, float, float);
float largesmall(float, float, float);

int main()
{
float side1, side2, side3, s;
cout << "Enter the sides; ";
cout << "\n";
cout << "Enter Side 1: ";
cin >> side1;
cout << "Enter Side 2: ";
cin >> side2;
cout << "Enter Side 3: ";
cin >> side3;
myFunction(side1, side2, side3);
float peri = perimeter(side1, side2, side3);
cout << "The perimeter is: " << peri << endl;
float ar = area(side1, side2, side3);
cout << "The area is: " << ar << endl;
float tp = type(side1, side2, side3);
cout << "\n";
float ls = largesmall(side1, side2, side3);
cout << "\n";
system("pause");
}

void myFunction(float x, float y, float z) {

cout << "Side x: " << x << endl;
cout << "Side y: " << y << endl;
cout << "Side z: " << z << endl;
}

float perimeter(float x, float y, float z) {
return x + y + z;
}

float area(float x, float y, float z)
{
float s;
s = (x + y + z) / 2;
return sqrt(s * (s - x) * (s - y) * (s - z));
}

float type(float x, float y, float z)
{
if (x == y && y == z)
{
cout << "Equilateral Triangle ";
}
else if (x == y || x == z || y == z)
{
cout << "Isosceles Triangle ";
}
else
{
cout << "Scalene Triangle ";
}
return(0);
}


float largesmall(float x, float y, float z)
{
if (x >= y && x >= z)
{
cout << "Side X is the largest side ";
}
else if (y >= x && y >= z)
{
cout << "Side Y is the largest side ";
}
else
{
cout << "Side Z is the largest side ";
}
cout << "\n";
if (x == y && y == z)
{
cout << "All sides are equal, so side Z,X,Y are all the same size ";
}
if (x == y && x > z)
{
cout << "Sides X and Y are both the largest sides ";
}
else if (y == z && y > x)
{
cout << "Sides Y and Z are both the largest sides ";
}
else if (z == x && z > y)
{
cout << "Sides Z and X are both the largest ";
}
cout << '\n';
if (x <= y && x <= z)
{
cout << "Side X is the smallest side ";
}
else if (y <= x && y <= z)
{
cout << "Side Y is the smallest side ";
}
else
{
cout << "Side Z is the smallest side ";
}
cout << "\n";
if (x == y && x < z)
{
cout << "Sides X and Y are both the smallest sides ";
}
else if (y == z && y < x)
{
cout << "Sides Y and Z are both the smallest sides ";
}
else if (z == x && z < y)
{
cout << "Sides Z and X are both the smallest sides ";
}
return(0);
}
Use fstream

1
2
3
4
5
6
7
#include <fstream>
using namespace std;

int main() {
  ofstream out("fileNameHere");
  out << "Hello World!";
}


Edit:

An example using your code:

1
2
3
4
5
6
7
#include <fstream>
void myFunction(float x, float y, float z) {
  ofstream fout("output.txt");
  fout << "Side x: " << x << endl;
  fout << "Side y: " << y << endl;
  fout << "Side z: " << z << endl;
}
Last edited on
Hello theforgottenone4,

I see you tried to use code tags, but something went wrong. Try these two links. They might help:
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

I found the second link to be the most help.

You need to pay more attention to your instructions
1
2
3
4
Call all the functions defined to generate a output report file with proper formatting
   using iomanip library
   rounding
   floating point numbers to 2 decimal values.

In line 1 "output report file" is a give away that you will need the header file "<fstream>" to write to a file. As highwayman has pointed out.

For what its worth when I was first learning about using input and output files I came up with this code:
1
2
3
4
5
6
7
8
9
10
const std::string outFileName{ "" };  // <--- Put file name here.

std::ofstream outFile(outFileName);

if (!outFile)
{
	std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional as is the header files.
	return 1;  //exit(1);  // If not in "main".
}

I found it useful in the beginning. The code can be shortened a bit when you have a better understanding of how it works. Line 8 is optional. Its something I use with my IDE. You may not need this.

"rounding" could use some clarification. Does this refer to using "setprecision" and 2 decimal places for rounding? Or could this refer to using "round" from the "cmath" header file?

Just because it says "floating point numbers" does not mean to use "float". "double" is the preferred floating point type.

I put this line near the beginning of "main":
std::cout << std::fixed << std::showpoint << std::setprecision(2); You only need to do this once and it affect every "cout" statement until it is changed. This comes from the "iomanip" header file.

The functions look OK except that you may want to output to the file along with the "cout" statement. And try to avoid using single letter variable names. i makes it hard to follow in the program. In "main" you used "side1" etc. Those same names are OK to use in your functions and have more meaning than "x", "y" and "z".

And a few blank lines in "main" make a big difference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int main()
{
	double side1, side2, side3/*, s*/; // <--- "s" is not used in "main".

	const std::string outFileName{ "" };  // <--- Put file name here.

	std::ofstream outFile(outFileName);

	if (!outFile)
	{
		std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional as is the header files.
		return 1;  //exit(1);  // If not in "main".
	}

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	std::cout << "Enter the sides; ";
	std::cout << "\n";
	std::cout << "Enter Side 1: ";
	std::cin >> side1;
	std::cout << "Enter Side 2: ";
	std::cin >> side2;
	std::cout << "Enter Side 3: ";
	std::cin >> side3;

	myFunction(side1, side2, side3);

	double peri = perimeter(side1, side2, side3);
	std::cout << "The perimeter is: " << peri << std::endl;

	double ar = area(side1, side2, side3);
	std::cout << "The area is: " << ar << std::endl;

	double tp = type(side1, side2, side3);
	std::cout << "\n";

	double ls = largesmall(side1, side2, side3);
	std::cout << "\n";

	// <--- Replacement for "system("pause");"
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}


Hope that helps,

Andy

Edit: type.
Last edited on
Alright, thanks for the reply. I got your code translated but there's no output in my text file. Here's what I have.


//Jordon Taylor - Triangles//
#include <iostream>
#include <fstream>
#include <iomanip>
#include <chrono>
#include <thread>

using namespace std;

void myFunction(float, float, float);
float perimeter(float, float, float);
float area(float, float, float);
float type(float, float, float);
float largesmall(float, float, float);

int main()
{
double side1, side2, side3, s;

const string outFileName{ "hi.txt" };

ofstream outFile(outFileName);

if (!outFile)
{
std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
return 1;
}

cout << std::fixed << std::showpoint << setprecision(2);

cout << "Enter the sides; ";
cout << "\n";
cout << "Enter Side 1: ";
cin >> side1;
cout << "Enter Side 2: ";
cin >> side2;
cout << "Enter Side 3: ";
cin >> side3;

myFunction(side1, side2, side3);

double peri = perimeter(side1, side2, side3);
std::cout << "The perimeter is: " << peri << std::endl;

double ar = area(side1, side2, side3);
std::cout << "The area is: " << ar << std::endl;

double tp = type(side1, side2, side3);
std::cout << "\n";

double ls = largesmall(side1, side2, side3);
std::cout << "\n";

// <--- Replacement for "system("pause");"
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();

return 0;
}

void myFunction(float x, float y, float z) {
ofstream fout("hi.txt");
fout << "Side x: " << x << endl;
fout << "Side y: " << y << endl;
fout << "Side z: " << z << endl;
}

float perimeter(float x, float y, float z) {
ofstream fout("hi.txt");
return x + y + z;
}

float area(float x, float y, float z)
{
float s;
s = (x + y + z) / 2;
return sqrt(s * (s - x) * (s - y) * (s - z));
}

float type(float x, float y, float z)
{
if (x == y && y == z)
{
cout << "Equilateral Triangle ";
}
else if (x == y || x == z || y == z)
{
cout << "Isosceles Triangle ";
}
else
{
cout << "Scalene Triangle ";
}
return(0);
}


float largesmall(float x, float y, float z)
{
if (x >= y && x >= z)
{
cout << "Side X is the largest side ";
}
else if (y >= x && y >= z)
{
cout << "Side Y is the largest side ";
}
else
{
cout << "Side Z is the largest side ";
}
cout << "\n";
if (x == y && y == z)
{
cout << "All sides are equal, so side Z,X,Y are all the same size ";
}
if (x == y && x > z)
{
cout << "Sides X and Y are both the largest sides ";
}
else if (y == z && y > x)
{
cout << "Sides Y and Z are both the largest sides ";
}
else if (z == x && z > y)
{
cout << "Sides Z and X are both the largest ";
}
cout << '\n';
if (x <= y && x <= z)
{
cout << "Side X is the smallest side ";
}
else if (y <= x && y <= z)
{
cout << "Side Y is the smallest side ";
}
else
{
cout << "Side Z is the smallest side ";
}
cout << "\n";
if (x == y && x < z)
{
cout << "Sides X and Y are both the smallest sides ";
}
else if (y == z && y < x)
{
cout << "Sides Y and Z are both the smallest sides ";
}
else if (z == x && z < y)
{
cout << "Sides Z and X are both the smallest sides ";
}
return(0);
}
Hello theforgottenone4,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Now that I have had the opportunity to test your program I see some of your problems and that does not include the new problems that have been created.

When I said that you should use "double" instead of "float" I meant everywhere not just in "main" using the code I showed you.

Now you have created the problem that "main" sends a "double" to a function which defines the variable as a "float". This will produce a warning that there could be possible data loss when you try to put something larger, (the "double"), into something smaller, (the "float"). This will not stop your program from running, but could produce an incorrect answer.

In "main" you have opened a file stream to your file, but in the function "myFunction" you start with opening the stream again. I did not sest that part, so I am not sure if that would even work.

Your choices are to open the file stream in "main" and pass that stream to the functions that need it, this is the better choice, or open the file stream in each function that will need it this creates redundancy which should be avoided.

The functions "perimeter" and "area" both return a value and work, but the functions "type" and "largesmall" both return a floating point number. Why? There is nothing in either function that requires a return value. These functions should be at the least "void" functions or return a "std::string". The code return(0); should be written as return(0.0); to be proper since it is a floating point number. Although you do not need it in the first place.

The whole of the "largeamall" function does not work wrll. The logic is wrong and does produce output that should bot be.

An example:

 Side x: 5.25
 Side y: 5.25
 Side z: 5.25

 The perimeter is: 15.75

 The area is: 13.78

 Equilateral Triangle

 Side X is the largest side at 5.25.
 All sides are equal, so side Z,X,Y are all the same size
 Side X is the smallest side at 5.25



 Press Enter to continue:


The line "All sides are equal, ..." says it all. Saying that "side x" is the longest and the shortest is a bit redundant. The if statements need rearranges or changed to form the proper output.

In the function "area" there is no square root in figuring the area. What I have found so far is A = 1/2 * B *Hb or A = 0.5 * B * Hb where B = base and Hb = height. Your use of the square root produces the wrong result. There is the use of square root when figuring the height, but alas I do not have a good understanding of that yet.

Out of the three variables which one do you consider the base and do you need something to figure the height before you use it? It does look like the square root is used in figuring the height. In which case what you have may work, but I am not sure.

Should you use the functions "sqrt" or "pow" you will need to include the header file "cmath". Do not relay on this being done for you through another header file.

Some parts of the program need to be rewritten and you need to decide how to deal with the file stream.

You say
but there's no output in my text file
. Of course there is no output because of the way you have opened the file stream twice. This is usually a problem. This is just one function, but should give you an idea:
1
2
3
4
5
6
7
8
9
10
void myFunction(std::ofstream& outFile, double side1, double side2, double side3)
{
	std::cout << "\n Side x: " << side1 << std::endl;
	std::cout << " Side y: " << side2 << std::endl;
	std::cout << " Side z: " << side3 << std::endl;

	outFile << "\n" << std::left << std::setw(19) << " Side x: " << std::right << std::setw(5) << side1 << std::endl;
	outFile << std::left << std::setw(19) << " Side y: " << std::right << std::setw(5) << side2 << std::endl;
	outFile << std::left << std::setw(19) << " Side z: " << std::right << std::setw(5) << side3 << std::endl;
}

By passing the file stream to the function it stays open in "main" and you just keep adding to the file. If you open the stream in the function you will have to tell it to append the file otherwise it would open the file and overwrite what is there starting from the beginning of the file and that is not what you want.

In the end it would be better to collect your data and call one function for your output be it to "cout" the file or both.

Hope that helps,

Andy
Topic archived. No new replies allowed.