Output prompt display to text file

Sep 6, 2013 at 6:04pm
The following code simply asks the user for their name and then prints "Hello, [name]!"

When run from a Windows command prompt it runs as expected with this output ("Jim" is input by the user):

What is your name? _ Jim
Hello, Jim!

I'd like to redirect the output (and the name inputted by the user) to a file. Essentially, I'd like to send exactly what is seen above to a text file and also see the program run in the command prompt. When I use this:

hello.exe > output.txt

the program doesn't display the question, "What is your name?" in the command prompt, but it is sent to the output.txt file. Then when a name is entered, the message "Hello, Jim!" is not displayed in the prompt, but it is sent to the text file. This is the contents of the text file:

What is your name? Hello, Jim!

It works as expected, but is there a way to see the program execute, then send exactly what is displayed in the command prompt to a text file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
#include <string>
using namespace std;

int main()
{
	// request user's name and store in a variable
	string name;
	cout << "What is your name? ";
	cin >> name;
	
	// print "Hello" and the user's name
	cout << "Hello, ";
	cout << name << "!" << endl;
	
	return 0;
}
Last edited on Sep 6, 2013 at 6:07pm
Sep 6, 2013 at 6:12pm
You want to use an output file stream for this.
http://www.cplusplus.com/reference/fstream/ofstream/?kw=ofstream

Generally speaking most of the time when you input a string you want to use getline also.

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	// request user's name and store in a variable
	string name;
	cout << "What is your name? ";
	cin >> name;
        cin.getline( cin , name );
	
	// print "Hello" and the user's name
	cout << "Hello, ";
	cout << name << "!" << endl;
        ofstream out( "filename.txt" ); //if the file doesn't exist it creates one
        //after the filename you can put flags for other things like appending
        //instead of writing over previous file
	out << "Hello, " << name << "!" << endl;
        //The file should close automatically but to be safe
        out.close();
	return 0;
}



edit sorry I did the c++11 version I think if you are using the older one you have to do something like
1
2
ofstream out;
out.open( "filename.txt" );
Last edited on Sep 6, 2013 at 6:13pm
Sep 6, 2013 at 6:40pm
Thanks for the help. I'm getting an error from this line:

cin.getline( cin, name );

This is the error:

[Error] no matching function for call to 'std::basic_istream<char>::getline(std::istream&, std::string&)'

I'm unsure what version of C++ I'm using, but I'm using Dev C++ Version 5.4.2.
Sep 6, 2013 at 7:00pm
Well I got rid of the error simply by using getline( cin, name ) instead of cin.getline( cin, name ).

edit: However, the program isn't working. It doesn't seem to be executing from line 17 down. The hello message is never displayed.
Last edited on Sep 6, 2013 at 7:05pm
Sep 6, 2013 at 7:12pm
whoops sorry yeah getline not cin.getline Are you trying to display it in the command prompt or output it to a text file?
Sep 6, 2013 at 7:18pm
Are you trying to display it in the command prompt or output it to a text file?

Why not both:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	// request user's name and store in a variable
	string name;
	cout << "What is your name? ";
	getline( cin , name );
	
	// print "Hello" and the user's name
	cout << "Hello, ";
	cout << name << "!" << endl;
        ofstream out( "filename.txt" ); //if the file doesn't exist it creates one
        //after the filename you can put flags for other things like appending
        //instead of writing over previous file
	out << "Hello, " << name << "!" << endl;
        //The file should close automatically but to be safe
        out.close();
	return 0;
}
Sep 6, 2013 at 8:10pm
Yeah, both is what I was shooting for. But, I was trying to get the command prompt to do the output to the text file. Basically I just want to send what was displayed in the command prompt to the text file; but I guess there is not really a way to do this.
Sep 6, 2013 at 8:11pm
Everytime you do cout << put a out << also ?same with cin
Sep 6, 2013 at 8:25pm
Yeah, I see how that would work, but I was trying to get the command prompt to do all the file stream work.

Similar to the redirect command: executable.exe > output.txt

But somehow displaying the entire program interaction on the screen and also sending it to the output.txt file.

I guess this might not be possible though.
Sep 6, 2013 at 8:39pm
You can copy the entire program to a text file as long as every cout call is also sent to a file and every cin call is also copied across. You're then basically make a log file of everything that happened.
Topic archived. No new replies allowed.