how to save output in a text file

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


#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))
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;
}

Look at this:
http://www.cplusplus.com/reference/iostream/fstream/

Edit: I posted the wrong link, this will make more sense now.
Last edited on
Thank you Browni, but i am a new c++ user and still need help :)
The main things you need to learn are opening and closing files, creating file pointers, and operators and functions used to read/write to a file. Here's an example of file in/out:

1
2
3
4
5
6
7
8
9
10
11
12
#include<fstream>

using namespace std;

main ()
{
    ofstream outfile;// declaration of file pointer named outfile
    outfile.open("filename", ios::out); // opens file named "filename" for output
    outfile << "Hello";//saves "Hello" to the outfile with the insertion operator
    outfile.close();// closes file; always do this when you are done using the file
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include<fstream>// needed for file in/out
#include<iostream>

using namespace std;

main ()
{
    ifstream infile;// declaration of file pointer named infile
    infile.open("filename", ios::in);// opens file named "filename" for input
    cout << getline(infile); // I'm not actually sure if this line will work
    infile.close();//closes file
    return 0;
}

This example is no substitute for a proper tutorial. There are many other things you need to learn.
hi, am new to this site............
Topic archived. No new replies allowed.