Saving What Is Typed While Running to a Text Document

Hi. I am fairly new to C++, and I was wondering how I would save a specific part of what is output from the computer to a text file? This is my code:
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
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string firstname, middlename, lastname, day, month, year, color, place, favoritetime, food, drink, game, placetoeat, book, electronicdevice, laptopbrand, desktopbrand, fullname, fulldateofbirth, favoritemeal;
    cout << "What is your first name?" << endl;
    getline(cin, firstname);
    cout << "What is your middle name?" << endl;
    getline(cin, middlename);
    cout << "What is your last name?" << endl;
    getline(cin, lastname);
    cout << "On what day were you born?" << endl;
    getline(cin, day);
    cout << "In what month were you born?" << endl;
    getline(cin, month);
    cout << "In what year were you born?" << endl;
    getline(cin, year);
    cout << "What is your favorite time of day? (Morning; Afternoon; Evening;)" << endl;
    getline(cin, favoritetime);
    cout << "What is your favorite color?" << endl;
    getline(cin, color);
    cout << "What is your favorite food?" << endl;
    getline(cin, food);
    cout << "What is your favorite drink?" << endl;
    getline(cin, drink);
    cout << "What is your favorite place to eat?" << endl;
    getline(cin, placetoeat);
    cout << "What is your favorite game?" << endl;
    getline(cin, game);
    cout << "What is your favorite electronic device? (Laptop, iPad, TV, etc.)" << endl;
    getline(cin, electronicdevice);
    cout << "What is your favorite laptop brand?" << endl;
    getline(cin, laptopbrand);
    cout << "What is your favorite desktop computer brand?" << endl;
    getline(cin, desktopbrand);
    fullname = firstname + " " + middlename + " " + lastname + ".";
    fulldateofbirth = month + " " + day + "," + " " + year + ".";
    food += " and ";
    drink += ",";
    favoritemeal = food + drink;
    favoritetime += ".";
    color += ".";
    placetoeat += ".";
    electronicdevice += ".";
    laptopbrand += ",";
    desktopbrand += ".";
    cout << "Processing Information..." << endl;
    Sleep(5000);
    cout << "Your full name is " << fullname << "You were born on " << fulldateofbirth << "Your favorite time of day is the " << favoritetime << endl << "Your favorite color is " << color << "Your favorite meal consists of " << favoritemeal << " and your favorite place to eat is " << placetoeat << endl << " Your favorite game is " << game << " and your favorite electronic device is a " << electronicdevice << " Your favorite laptop brand is " << laptopbrand << " and your favorite desktop brand is " << desktopbrand << " Thank you for taking the Introduction quiz!" << endl;

    return 0;
}


Does anyone know how I would do this? I need an answer pretty quickly.
Last edited on
First add #include <fstream> at the top of your file.

Then you can simply do this
1
2
3
4
5
6
        ofstream File;
	File.open("Info.txt"); 

	File << "Any info here"  // (in your case that long sentence) 

	File.close();


By default this will create the text file where ever the program was ran from, if you want to save it at a different place you can do

1
2
3
4
	string FileName = "C:\\Users\\Desktop\\Info.txt";   // Whatever filename you need (or you can have user enter it) 

	ofstream File;
	File.open(FileName.c_str()); 

Last edited on
Thank You!!! =) Wait...... My sentence has alot of quotation marks in it. how can I put my sentence in between the quotation marks without completely messing up my other code?
Last edited on
My mistake, in your case you just need to put the sentence as it will recognize all of the variable types etc.
Ok, thanks!
I did what you said, and it makes the text document exactly where I want it to with the name I want it to, but if I open up the document, there is nothing there. (No Long Sentence Telling Me What My Favorite Stuff Is, Just a Blank Page)
1
2
3
4
5
6
        ofstream File;
	File.open("Info.txt"); 

	File << "Your full name is " << fullname << "You were born on " << fulldateofbirth << "Your favorite time of day is the " << favoritetime << "Your favorite color is " << color << "Your favorite meal consists of " << favoritemeal << " and your favorite place to eat is " << placetoeat << "your favorite game is " << game << " and your favorite electronic device is a " << electronicdevice << "Your favorite laptop brand is " << laptopbrand << " and your favorite desktop brand is " << desktopbrand << "Thank you for taking the Introduction quiz!" << endl;  // (in your case that long sentence) 

	File.close();


Do you have it set up exactly like that? I have tried running it and it all runs correctly.
Of course it needs to be at the end of your code, after everything has been entered by the user.
Last edited on
Yeah, that fixed it. I put the code in incorrectly. Thank you! =)
Oh, great. Now I have another problem. After It does the part that says Processing Information... it shuts off right away, before I press any buttons, giving me no time to read what it has said. Could this be due to the "File.Close" function? Do you know how I could stop this?
That is because you have nothing to pause the console window after it is done showing all of the data (it's not related to fstream)

There are a few lines you can use at the end of your program (right before return 0) to stop this...
- system("PAUSE"); (highly disliked by most programmers and I wouldn't recommend it)
- cin.get() will pause the program until you enter something

http://www.cplusplus.com/forum/beginner/1988/

A few others are probably mentioned in that thread as well.
Last edited on
Ok, thanks.
Topic archived. No new replies allowed.