I have a C++ program I wrote and I would like to save it to a .txt file. I would
ID: 640128 • Letter: I
Question
I have a C++ program I wrote and I would like to save it to a .txt file. I would like it to look like the print out. I know I can do the fstream and save it line by line but I there must be an easier way to save it all...
Here is my code:
MAIN.CPP ----
//Use this driver file to TEST your week 6 assignment...
// COMP 220 Week 6 Overloaded Operators
// Complex Math Operator Overload Lab Assignment
// This program asks the user to enter two complex numbers in real, imag format
// and then uses overloaded operators to perform complex math operations.
#include <iomanip>
#include <iostream>
#include <math.h>
#include <windows.h>
#include "complexnumber.h"
using namespace std;
int main(void)
{
cout << "This program performs complex math addition, subtraction, "
<< endl << "multiplication and division operations on complex data"
<< endl << "utilizing operator overloading." << endl << endl;
cout << "Instantiate three objects of class complex number" << endl << endl;
ComplexNumber compOne(5.0, 6.0);
ComplexNumber compTwo(3.0, 9.0);
ComplexNumber compThree;
cout << fixed << setprecision(3) << showpoint;
cout << "Comp One real part = " << setw(8) << compOne.getRealPart() << endl;
cout << "Comp One imag part = " << setw(8) << compOne.getImagPart() << "j" << endl;
cout << "Comp Two real part = " << setw(8) << compTwo.getRealPart() << endl;
cout << "Comp Two imag part = " << setw(8) << compTwo.getImagPart() << "j" << endl;
cout << "Comp Three real part = " << setw(8) << compThree.getRealPart() << endl;
cout << "Comp Three imag part = " << setw(8) << compThree.getImagPart() << "j" << endl;
cout << endl << "Set compThree equal to compOne using overloaded assignment operator." << endl << endl;
compThree = compOne;
cout << "Comp One real part = " << setw(8) << compOne.getRealPart() << endl;
cout << "Comp One imag part = " << setw(8) << compOne.getImagPart() << "j" << endl;
cout << "Comp Two real part = " << setw(8) << compTwo.getRealPart() << endl;
cout << "Comp Two imag part = " << setw(8) << compTwo.getImagPart() << "j" << endl;
cout << "Comp Three real part = " << setw(8) << compThree.getRealPart() << endl;
cout << "Comp Three imag part = " << setw(8) << compThree.getImagPart() << "j" << endl;
cout << endl << "Set compThree = compOne + compTwo using overloaded + operator." << endl << endl;
compThree = compOne + compTwo;
cout << "Comp One real part = " << setw(8) << compOne.getRealPart() << endl;
cout << "Comp One imag part = " << setw(8) << compOne.getImagPart() << "j" << endl;
cout << "Comp Two real part = " << setw(8) << compTwo.getRealPart() << endl;
cout << "Comp Two imag part = " << setw(8) << compTwo.getImagPart() << "j" << endl;
cout << "Comp Three real part = " << setw(8) << compThree.getRealPart() << endl;
cout << "Comp Three imag part = " << setw(8) << compThree.getImagPart() << "j" << endl;
cout << endl << "Set compThree = compOne - compTwo using overloaded - operator." << endl << endl;
compThree = compOne - compTwo;
cout << "Comp One real part = " << setw(8) << compOne.getRealPart() << endl;
cout << "Comp One imag part = " << setw(8) << compOne.getImagPart() << "j" << endl;
cout << "Comp Two real part = " << setw(8) << compTwo.getRealPart() << endl;
cout << "Comp Two imag part = " << setw(8) << compTwo.getImagPart() << "j" << endl;
cout << "Comp Three real part = " << setw(8) << compThree.getRealPart() << endl;
cout << "Comp Three imag part = " << setw(8) << compThree.getImagPart() << "j" << endl;
cout << endl << "Set compThree = compOne * compTwo using overloaded * operator." << endl << endl;
compThree = compOne * compTwo;
cout << "Comp One real part = " << setw(8) << compOne.getRealPart() << endl;
cout << "Comp One imag part = " << setw(8) << compOne.getImagPart() << "j" << endl;
cout << "Comp Two real part = " << setw(8) << compTwo.getRealPart() << endl;
cout << "Comp Two imag part = " << setw(8) << compTwo.getImagPart() << "j" << endl;
cout << "Comp Three real part = " << setw(8) << compThree.getRealPart() << endl;
cout << "Comp Three imag part = " << setw(8) << compThree.getImagPart() << "j" << endl;
cout << endl << "Set compThree = compOne / compTwo using overloaded / operator." << endl << endl;
compThree = compOne / compTwo;
cout << "Comp One real part = " << setw(8) << compOne.getRealPart() << endl;
cout << "Comp One imag part = " << setw(8) << compOne.getImagPart() << "j" << endl;
cout << "Comp Two real part = " << setw(8) << compTwo.getRealPart() << endl;
cout << "Comp Two imag part = " << setw(8) << compTwo.getImagPart() << "j" << endl;
cout << "Comp Three real part = " << setw(8) << compThree.getRealPart() << endl;
cout << "Comp Three imag part = " << setw(8) << compThree.getImagPart() << "j" << endl;
cout << endl << "End Complex Number Program" << endl << endl;
{
cout << " Press any key to exit" << endl;
cin.ignore(1);
}
}
COMPLEXNUMBER.CPP -----
#include "Complexnumber.h"
ComplexNumber:: ComplexNumber(){
real = 0;
imag = 0;
}
ComplexNumber:: ComplexNumber(float i, float j){
real = i;
imag = j;
}
ComplexNumber ComplexNumber:: operator+(const ComplexNumber& other)
{
ComplexNumber result;
result.real= real+other.real;
result.imag= imag + other.imag;
return result;
}
ComplexNumber ComplexNumber:: operator-(const ComplexNumber& other)
{
ComplexNumber result;
result.real= real-other.real;
result.imag= imag - other.imag;
return result;
}
ComplexNumber& ComplexNumber:: operator=(const ComplexNumber& other)
{
real= other.real;
imag = other.imag;
return *this;
}
ComplexNumber ComplexNumber:: operator*(const ComplexNumber& other)
{
ComplexNumber result;
result.real=(real*other.real)-(imag*other.imag);
result.imag = (real*other.imag)+(imag*other.real);
return result;
}
ComplexNumber ComplexNumber:: operator/(const ComplexNumber& other)
{
float div=(other.real*other.real) + (other.imag*other.imag);
ComplexNumber result;
result.real=(real*other.real)+(imag*other.imag);
result.real/=div;
result.imag = (imag*other.real)-(real*other.imag);
result.imag/=div;
return result;
}
float ComplexNumber:: getRealPart(){
return real;
}
float ComplexNumber:: getImagPart(){
return imag;
}
COMPLEXNUMBER.H-----
#ifndef COMPLEX_NUMBER_H
#define COMPLEX_NUBMER_H
class ComplexNumber{
private:
float real;
float imag;
public:
ComplexNumber();
ComplexNumber(float, float);
ComplexNumber operator+(const ComplexNumber& other);
ComplexNumber operator-(const ComplexNumber& other);
ComplexNumber& operator=(const ComplexNumber& other);
ComplexNumber operator*(const ComplexNumber& other);
ComplexNumber operator/(const ComplexNumber& other);
float getRealPart();
float getImagPart();
};
#endif
Explanation / Answer
There are many ways to write to txt file
1) The easiest way to do so without programming would be (in case you are using turbo compiler) to save the file at a location with c++ extension and then open microsoft visual c++ and go to that location and open the file with vc++ and you are free to copy and paste the code in word/wordpad or any text editor. :)
2) you can use streamwriter which provides option for formatted output.At present you might be using a method to writeline. I suggest you use writemethod(char) where you end till you reach EOF char.
I hope this the answer you were looking for, if not please let me know.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.