storing data in a txt

im trying to make a program that store a person firstname, lastname, birthday etc in a txt. how would i make the txt file be named to the persons lastname and firstname? Also make a new file per person. Im thinking i would use an array or structure but not too sure. Thanks for the help

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
	string firstname, lastname, birthyear, birthmonth, date;
	cout << "Whats your firstname? "; getline(cin, firstname);
	cout << "Whats your lastname? "; getline(cin, lastname);
	cout << "Whats your birth year? "; getline(cin, birthyear);
	cout << "Whats your birth month? "; getline(cin, birthmonth);
	cout << "Whats your birth date? "; getline(cin, date);


	ofstream myfile;
	myfile.open("name.txt");
	myfile << firstname;
	myfile << lastname;
	myfile << birthyear;
	myfile << birthmonth;
	myfile << date;
	myfile.close();

	return 0;
}
If you try and open a file that does not exist, it will be created for you. I made an example of how you can name a file your first and last name.

1
2
3
4
ofstream myFile;
string myName = "Tarik";
string myLastName = "Neaj";
myFile.open(myName + "_" + myLastName + ".txt");


A file will be created in your project named Tarik_Neaj.txt
how would i make the txt file be named to the persons lastname and firstname?
ofstream myfile (lastname + firstname);

As I said in my post, it's an example. What you're asking for is exactly what I and @Moschops gave you, now you just have to use it, you can remove the predefined strings, and use the user defined strings.

1
2
3
4
5
6
7
8
string firstname, lastname;

cout << "Whats your firstname? "; getline(cin, firstname);
cout << "Whats your lastname? "; getline(cin, lastname);

ofstream myfile;

myfile.open(firstname+ "_" + lastname+ ".txt");
Last edited on
@tarikneaj thankyou that helped alot
Hi,

I coded this real quick for you.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void createFile()
{
	string firstname, lastname, birthyear, birthmonth, date;
	cout << "Whats your firstname?" << endl;
	getline(cin, firstname);
	cout << "Whats your lastname?" << endl;
	getline(cin, lastname);
	cout << "Whats your birthyear?" << endl;
	getline(cin, birthyear);
	cout << "Whats your birthmonth?" << endl;
	getline(cin, birthmonth);
	cout << "Whats the date?" << endl;
	getline(cin, date);
	ofstream myfile(firstname + "_" + lastname + ".txt");
	if (myfile.is_open())
	{
		myfile << firstname << endl;
		myfile << lastname << endl;
		myfile << birthyear << endl;
		myfile << birthmonth << endl;
		myfile << date;
	}
	else
	{
		cout << "Unable to open file";
	}

}

int main()
{
	createFile();

	cout << "Press ENTER to continue...";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
Whats your firstname?
name1
Whats your lastanem?
name2
Whats your birthyear?
1993
Whats your birthmonth?
April
Whats the date?
12/20/2015
Press ENTER to continue...


name1_name2.txt:
1
2
3
4
5
name1
name2
1993
April
12/20/2015
Topic archived. No new replies allowed.