If I'm understanding the request right, the code is already text. You can just save it as a text file. (Though honestly, if someone is requesting a txt file of code instead of a cpp file I'd be wondering how much they know about programming.)
As far as I know,
#include the fstream library
create an ofstream object which takes two arguments, a path and ios::out
put to the ofstream object
close the object using object name.close().
I'm new to c++ too, but that's how I learned it recently
The simple way is redirection. cout is standard output, which can be a file. You do this in the command line: $ myProgram >output.txt
Similarly, cin can become from a file too: $ myProgram <input.txt
Maybe you want to output to screen and file, then you need fstream:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <fstream>
#include <iostream>
using namepsace std;
int main()
{
// output file
ofstream out ("output.txt");
// to create a file for input: ifstream in("input.txt");
// Use ofstream like cout
out << "Hello World\n";
cout << "I wrote data to the file\n";
// ifstream is just like cin:
out.close();
return 0;
}