C++ help with writing the console output to file

Hello there. I have this code that basically generates a random conversation between me and the computer. The computer will read a file that contains a name in order to name it self and then carry on the conversation. At the end I need everything from the console output to be written to another file. I have tried various methods I found online so far to no avail.

My current code in full is as follows:

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
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
#include<time.h>

using namespace std;

void random()
{
	srand(time(NULL));
	int step;
	for (int i = 2; i < 3; i++)
	{
		step = rand() % 4 + 1;
		if (step == 1)
		{
			cout << "Well ain't that peachy." << endl;
		}
		else if (step == 2)
		{
			cout << "Tell me more about your mom instead?" << endl;
		}
		else if (step == 3)
		{
			cout << "Got any hot sisters?" << endl;
		}
		else if (step == 4)
		{
			cout << "Kill yourself! Now tell me how did that feel?" << endl;
		}
	}
}

int main()
{
	string ifilename, ofilename, line, str1, str2, str3, str4, str5, str6, str7;
	ifstream inFile, checkOutFile;
	ofstream outFile;
	char response;
	// Input file
	cout << "Please enter the name of the file you wish to read : ";
	cin >> ifilename;
	inFile.open(ifilename.c_str());
	if (inFile.fail())
	{
		cout << "The file " << ifilename << " was not successfully opened." << endl;
		cout << "Please check the path and name of the file. " << endl;
		exit(1);
	}
	else
	{
		cout << "The file is successfully opened." << endl;
	}
	// Output file
	cout << "Please enter the name of the file you wish to write : ";
	cin >> ofilename;
	checkOutFile.open(ofilename.c_str());
	if (!checkOutFile.fail())
	{
		cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite it? (y/n) : ";
		cin >> response;
		if (tolower(response) == 'n')
		{
			cout << "The existing file will not be overwritten. " << endl;
			exit(1);
		}
	}
	outFile.open(ofilename.c_str());
	if (outFile.fail())
	{
		cout << "The file " << ofilename << " was not successfully opened." << endl;
		cout << "Please check the path and name of the file. " << endl;
		exit(1);
	}
	else
	{
		cout << "The file is successfully opened." << endl;
	}

	// Copy file contents from inFile to outFile
	while (getline(inFile, line))
	{
		cout << "Hello my name is " << line << ". What is your name?" << endl;
		cout << "";
		cin.ignore();
		getline(cin, str1);
		
		cout << "Hi " << str1 << ". What does your loser ass like to do for fun?" << endl;
		cout << "";
		cin.ignore();
		getline(cin, str2);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str3);
		
		random();
		cout << "";
		outFile;
		cin.ignore();
		getline(cin, str4);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str5);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str6);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str7);
		
		cout << "Well I am sorry but I have to go. But your mom is calling me." << endl;
	}
	// Close files
	inFile.close();
	outFile.close();
} // main 


The part that generates the console output being:

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
while (getline(inFile, line))
	{
		cout << "Hello my name is " << line << ". What is your name?" << endl;
		cout << "";
		cin.ignore();
		getline(cin, str1);
		
		cout << "Hi " << str1 << ". What does your loser ass like to do for fun?" << endl;
		cout << "";
		cin.ignore();
		getline(cin, str2);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str3);
		
		random();
		cout << "";
		outFile;
		cin.ignore();
		getline(cin, str4);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str5);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str6);
		
		random();
		cout << "";
		cin.ignore();
		getline(cin, str7);
		
		cout << "Well I am sorry but I have to go. But your mom is calling me." << endl;
	}


The current questions and such are just place holders so don't worry about the weirdness of it all. I would greatly appreciate any help.
Instead of using cout ("console out"), use the ofstream variable you have already declared (outFile). In your implementations, you can definitely have a cout AND an outFile, that way, what is displayed by the "computer" on the console is echoed in the file.
Topic archived. No new replies allowed.