Creating file name functions

So I know that this is a very basic question, but I've been stuck trying to figure this out for a few days now.

I'm trying to write a program in C++ that deals with creating accounts and stuff like that. I'm trying to create a function that takes the username (which is a string) and formats it into a txt file which would be username.txt (also a string). But it has to be able to change with each new username that is created. Can anybody help?
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>

int main ()
{
   std::cout << "What's the name? ";
   std::string name;
   std::cin >> name;

   std::string file_name = name + ".txt";
   std::ofstream ofs(file_name);
   
   if (ofs.is_open())
   {
      ofs << "You entered the name " << name << "\n";
   }

   ofs.close();
}


Enter "joe" and a "joe.txt" file is created.
Last edited on
Topic archived. No new replies allowed.