how to save output in a text file

Could someone tell me how to get the output of this program into a text file ?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;

int main(void)
{
	double x3, x2, x1, f;
	double a, b, p;
	double fa, fb, fp;
	double tol;
	int n, i;
	char indicator = 'n';
	int retry;

selection:

	cout << endl
		<< " Enter the value of x^3 coeff :";
	cin >> x3;

	cout << endl
		<< " Enter the value of the X^2 coeff :";
	cin >> x2;

	cout << endl
		<< " Enter the value of x coeff :";
	cin >> x1;

	cout << endl
		<< " Enter the value of free coeff :";
	cin >> f;

	cout << endl
		<< " Insert the number of iterations :";
	cin >> n;

	cout << endl
		<< " Enter the wanted tolerance :";
	cin >> tol;
    
	cout << endl
		<< " Enter the value of a :";
	cin >> a;

	cout << endl
		<< " Enter the value of b :";
	cin >> b;

	p = (a+b)/2;

	fa = x3 * pow(a,3) + x2 * pow(a,2) + x1 * a + f;
	fb = x3 * pow(b,3) + x2 * pow(b,2) + x1 * b + f;
	fp = x3 * pow(p,3) + x2 * pow(p,2) + x1 * p + f;
	
	

	for (i = 1; i < n && tol < abs(fp) && a < b; i++)
	{

		p = (a+b)/2;
	fa = x3 * pow(a,3) + x2 * pow(a,2) + x1 * a + f;
	fb = x3 * pow(b,3) + x2 * pow(b,2) + x1 * b + f;
	fp = x3 * pow(p,3) + x2 * pow(p,2) + x1 * p + f;
	
		cout << endl
		<< "Iteration number " << i;
	cout <<endl
		<< "~~~~~~~~~~~~~~~~~~~";
	cout <<endl
		<< "a = " <<a;
	cout <<endl
		<< "b = " <<b;
	cout <<endl
		<< "p = " <<p;
	cout <<endl
		<< "                 ";
	cout <<endl
		<< "f(a) = " <<fa;
	cout <<endl
		<< "f(b) = " <<fb;
	cout <<endl
		<< "f(p) = " <<fp;
	cout <<endl
		<< "                 ";
	cout << endl
		<< "Solution = " <<p;
	
	cout << endl 
		<<"______________________";

	if ((fa < 0) && (fp < 0) || ((fa > 0) && (fp > 0 )))
		a = p, b = b;
	else
		a = a, b = p;

	
	
	}
		
	cout<<endl
		<<"Select the number of the desired option.\n";
    cout<<"1. Try another problem\n";
    cout<<"2. Exit the program\n";
    cout<<">>. ";
    cin>>retry;

    switch (retry) {
    case 1:
    goto selection;

    case 2:
    goto exit;
	
	}
	exit:
return 0;
}
Please i want a solution for this case, and i will learn iostream issue latter
thanks in advance.
You create a new output filestream (ofstream) like this:
 
ofstream ofs("The_name_of_the_file_you_want_as_output");


Then you can use ofs just like you would cout. At the end of the program, you call ofs.close() and you're done.
Oh and you have to include fstream.

Btw: instead of including math.h, include cmath. Technically they're the same thing, but the .h files are just for compatibility with older C++ programs, the extensionless headers are now standard.
Last edited on
Hello!

Just a few things to note, khaled124. We don't provide complete solutions for ANY problem. It's kind-of an unwritten rule around here. :)

Second, we actually have a tutorial on file I/O on this site! It's kinda buried, but it's here: http://cplusplus.com/doc/tutorial/files/

Third, I wish you the best of luck!

-Albatross
Thanks for reply hanst99.

Albatross I understand your opinion, but I was hopless to find the solution of this problem

Any way thank you all
Topic archived. No new replies allowed.