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