Sending terminal output to text file

I have to do an assignment where I make a diamond in C++ with asterisks and output the result to the terminal and a .txt file. The diamond shows up fine when I run the program but it only displays a straight vertical line of asterisks in the text file. I have to pull the number of lines from an input file that's why there's an ifstream
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
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	ifstream input("input_diamond.txt");
	ofstream output("output_diamond.txt");

	int n, c, k, space = 1;

	input >> n;
	
	space = n - 1;
	

	for (k = 1; k <= n; k++)
	{
		for (c = 1; c <= space; c++)
			cout << " ";
			output << " ";

		space--;

		for (c = 1; c <= 2 * k - 1; c++)
			cout << "*";
			output << "*";

		cout << "\n";
		output << "\n";
	}

	space = 1;

	for (k = 1; k <= n - 1; k++)
	{
		for (c = 1; c <= space; c++)
			cout << " ";
			output << " ";

		space++;

		for (c = 1; c <= 2 * (n - k) - 1; c++)
			cout << "*";
			output << "*";

		cout << "\n";
		output << "\n";
	}

	input.close();
	output.close();

	return 0;
}
You need to execute both cout << ... and output << ... within the body of the loops.

In your for loops, place braces around the body of the loop; make it a compound statement.
For example:
1
2
3
4
5
6
7
8
9
10
11
for (k = 1; k <= n; k++)
{
    for (c = 1; c <= space; c++)
    { // *** added
        
        cout << " ";
        output << " ";
        
    } // *** added
    
    // ... 
Topic archived. No new replies allowed.