How to output console display to a txt.file?

Hello people!
i am currently doing a complex number calculator ,and i wish to output my data to a txt.file .Can anyone help me cause i tried fstream and it doesnt work.However the txt.file was created but no text was output.
Below is the program:
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<iomanip>
#include<cmath>
#include<iostream>
#include<fstream>
using namespace std;
#define PI 3.14159265358979323
double z,x;

class Complex
{
protected:
	double real1,img1,real2,img2;
	char sign1,sign2,j1,j2;

public :
	void getValue();
	void showpolar(double &r,double &d);
	void addValue(double &z,double &x);
	void divValue(double &z,double &x);
	void showValue(double &z,double &x);
	void showdiv(double &z,double &x);

};


void Complex::getValue()
{
	number1:
	cout<<"Enter the first complex number Z1 in form of (x+jy):";
	cin>>real1>>sign1>>j1>>img1;
	if(sign1!='+')
             
    {
		img1=-img1;
                 
        if(sign1!='-')
                 
        {
                 
            cout << "\n\tOnly + and - are allowed.\n" << endl;
             
            goto number1; 
                 
        }
             
    }
 
            if(j1!='j')
             
        {
                cout << "\nOnly Letter 'j' is allowed.\n" << endl;
                 
                goto number1;
        }

	number2:       
         
        cout<<"\n\nEnter the first complex number Z2 in form of (x+jy):";
                 
        cin>>real2>>sign2>>j2>>img2;
 
        if(sign2!='+')
             
        {
			img2=-img2;
            if(sign2!='-')
                 
            {
                    cout << "\tOnly + and - are allowed.\n" << endl;
                     
                    goto number2;
            }
             
        }
 
 
        if(j2!='j')
             
             
        {
                 
            cout << "\tOnly Letter 'j' is allowed.\n" << endl;
                 
            goto number2;
             
		}
}
void Complex::addValue(double &z,double &x)
{
	z=real1+real2;
    x=img1+img2;	 
}
void Complex::divValue(double &z,double &x)
{
	z=((real1*real2-img1*img2)*(real1+real2)+(img1*real2+img2*real1)*(img1+img2))/((pow(real1+real2,2))+(pow(img1+img2,2)));
	x=((img1*real2+img2*real1)*(real1+real2)-(real1*real2-img1*img2)*(img1+img2))/((pow(real1+real2,2))+(pow(img1+img2,2)));
}

void Complex::showValue(double &z,double &x)
{
	if ( x>= 0 )
      cout << "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<< z << " + j" <<setprecision(3)<< x<<endl;
	 
   else
      cout << "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<<z << " - j" <<setprecision(3)<< x*(-1)<<endl;
	  
	
}
void Complex::showdiv(double &z,double &x)
{
	if ( x>= 0 )
      cout << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<< z << " + j" <<setprecision(3)<< x <<endl;
	
   else
      cout << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<<z << " - j" <<setprecision(3)<< x*(-1) <<endl;
	  
}

class Polar:public Complex //inheritant from class Complex
{
    protected:
	double r,d,r2,d2;
    public:
	void PolarValue();
	void ShowPolar();
};
void Polar::PolarValue()
{   
	r=sqrt(pow(real1,2)+pow(img1,2));
	d=atan(img1/real1)*(180/PI);

	r2=sqrt(pow(real2,2)+pow(img2,2));
    d2=atan(img2/real2)*(180/PI);

    


}
void Polar::ShowPolar()
{ 
	cout<<"\n\n(Polar form Z1= "<<r<<" < "<<d<<" degree)"<<endl;
	cout<<"(Polar form Z2= "<<r2<<" < "<<d2<<" degree)"<<endl;


}

int main()
{
	std::fstream fs;
    fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
	Polar p;
	p.getValue();
	p.addValue(z,x);
	p.showValue(z,x);
	p.divValue(z,x);
	p.showdiv(z,x);
	p.PolarValue();
	p.ShowPolar();
	fs.close();

}
Last edited on
1) Please use code tags to make your code readable:

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

2) How do you know that fstream isn't working? You never write anything to fs. You open it in main, then close it, but at no point do you ever write anything to it
Last edited on
Hello MikeyBoy so what can i do so that i can output the console display to a txt.file ?
fstream is a stream, and works like 'cout'.

1
2
ofstream file("asd.txt");
file << "asd";


is it right to do this way? any better option to recommend ?
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<iomanip>
#include<cmath>
#include<iostream>
#include<fstream>
using namespace std;
#define PI 3.14159265358979323
double z,z2,x,x2;

class Complex
{
protected:
	double real1,img1,real2,img2;
	char sign1,sign2,j1,j2;

public :
	void getValue();
	void seriesValue(double &z,double &x);
	void parallelValue(double &z,double &x);
	void showSeries(double &z,double &x);
	void showParallel(double &z,double &x);


};


void Complex::getValue()
{
	number1:
	cout<<"Enter the first complex number Z1 in form of (x+jy):";
	cin>>real1>>sign1>>j1>>img1;
	if(sign1!='+')
             
    {
		img1=-img1;
                 
        if(sign1!='-')
                 
        {
                 
            cout << "\n\tOnly + and - are allowed.\n" << endl;
             
            goto number1; 
                 
        }
             
    }
 
            if(j1!='j')
             
        {
                cout << "\nOnly Letter 'j' is allowed.\n" << endl;
                 
                goto number1;
        }

	number2:       
         
        cout<<"\n\nEnter the first complex number Z2 in form of (x+jy):";
                 
        cin>>real2>>sign2>>j2>>img2;
 
        if(sign2!='+')
             
        {
			img2=-img2;
            if(sign2!='-')
                 
            {
                    cout << "\tOnly + and - are allowed.\n" << endl;
                     
                    goto number2;
            }
             
        }
 
 
        if(j2!='j')
             
             
        {
                 
            cout << "\tOnly Letter 'j' is allowed.\n" << endl;
                 
            goto number2;
             
		}
}
void Complex::seriesValue(double &z,double &x)
{
	z=real1+real2;
    x=img1+img2;	 
}
void Complex::parallelValue(double &z2,double &x2)
{
	z2=((real1*real2-img1*img2)*(real1+real2)+(img1*real2+img2*real1)*(img1+img2))/((pow(real1+real2,2))+(pow(img1+img2,2)));
	x2=((img1*real2+img2*real1)*(real1+real2)-(real1*real2-img1*img2)*(img1+img2))/((pow(real1+real2,2))+(pow(img1+img2,2)));
}

void Complex::showSeries(double &z,double &x)
{
	
	if ( x>= 0 )
	{
      cout << "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<< z << " + j" <<setprecision(3)<< x<<endl;
	}
   else
	 
     cout<< "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<<z << " - j" <<setprecision(3)<< x*(-1)<<endl;
	 
}
void Complex::showParallel(double &z2,double &x2)
{

	if ( x2>= 0 )
      cout << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<< z2 << " + j" <<setprecision(3)<< x2 <<endl;
	
   else
      cout << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<<z2 << " - j" <<setprecision(3)<< x2*(-1) <<endl;
	 
}

class Polar:public Complex //inheritant from class Complex
{
    protected:
	double r,d,r2,d2;
    public:
	void PolarValue();
	void ShowPolar();
	void printdata();
};
void Polar::PolarValue()
{   
	r=sqrt(pow(real1,2)+pow(img1,2));
	d=atan(img1/real1)*(180/PI);

	r2=sqrt(pow(real2,2)+pow(img2,2));
    d2=atan(img2/real2)*(180/PI);

    


}
void Polar::ShowPolar()
{
	cout<<"\n\n(Polar form Z1= "<<r<<" < "<<d<<" degree)"<<endl;
	cout<<"(Polar form Z2= "<<r2<<" < "<<d2<<" degree)"<<endl;

}
void Polar::printdata()
{
	ofstream myfile;
	myfile.open("C:\\temp\\complex.txt");
	myfile<<"Output data of Series & Parallel impedance of Z1 & Z2"<<endl<<endl;
	if (sign1=='-')
	
		myfile<<"Enter the first complex number Z1 in form of (x+jy):"<<real1<<sign1<<j1<<img1*-1;
	else
		myfile<<"Enter the first complex number Z1 in form of (x+jy):"<<real1<<sign1<<j1<<img1;
	
	if (sign2=='-')
	
	   myfile<<"\n\nEnter the first complex number Z2 in form of (x+jy):"<<real2<<sign2<<j2<<img2*-1;
	else
	   myfile<<"\n\nEnter the first complex number Z2 in form of (x+jy):"<<real2<<sign2<<j2<<img2;
    

	
	
	if ( x>= 0 )
      myfile << "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<< z << " + j" <<setprecision(3)<< x<<endl;
   else
      myfile<< "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<<z << " - j" <<setprecision(3)<< x*(-1)<<endl;
	
	if ( x2>= 0 )
      myfile << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<< z2 << " + j" <<setprecision(3)<< x2 <<endl;
	
   else
      myfile << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<<z2 << " - j" <<setprecision(3)<< x2*(-1) <<endl;
	
	myfile<<"\n\n(Polar form Z1= "<<r<<" < "<<d<<" degree)"<<endl;
	myfile<<"(Polar form Z2= "<<r2<<" < "<<d2<<" degree)"<<endl;
	myfile.close();
}


int main()
{
	
	Polar p;
	p.getValue();
	p.seriesValue(z,x);
	p.showSeries(z,x);
	p.parallelValue(z2,x2);
	p.showParallel(z2,x2);
	p.PolarValue();
	p.ShowPolar();
	p.printdata();
	
	

}
Last edited on
> How to output console display to a txt.file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

int main()
{
    std::cout << "1. this is written out to stdout\n" << std::flush ;

    {
        // redirect stdout to a file
        std::ofstream file( "outfile.txt" ) ;
        auto oldbuf = std::cout.rdbuf( file.rdbuf() ) ;

        std::cout << "2. this is written out to the file outfile.txt\n" << std::flush ;

        // restore the old behaviour of std::cout
        std::cout.rdbuf(oldbuf) ;
    }
    std::cout << "3. output is now back  to stdout\n" ;
}
JLBorges redefines what std::cout does. Other code does not need changes.

Even simpler is redirection outside the program. Say, your binary has name 'foo'. You run it now:
foo

Simply tell the shell to redirect stdout:
foo > file.txt

Topic archived. No new replies allowed.