File not creating

So i've made a program that asks you for a filename and then creates a file with the name you've said, but it doesn't create nothing

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  string Name;

  cout << "File Name: ";
  std::getline (std::cin,Name);

  ofstream File(Name + ".txt");
  File.close();
}
Last edited on
It worked fine on my system. I typed bob and then bob.txt appeared.
Unrelated: I like that you are using std::, but you are not using it everywhere and using namespace std; is still at the top
Last edited on
Well on mine, it's just creating a file like this : ".txt".
I think it doesn't regist the Name..
What did you type in?
test
That should work. What compiler/OS do you have?
VS 2012, windows 7
Can you add std::cout << "Name: " << Name << std::endl; between the std::getline and opening the file? I'm curious if for some strange reason it is blank.
Hello vaiandrept4,

I would add to kevinkjt2000 suggestion after your getline() add the .txt with Name += ".txt"; then see what name you end up with.

FYI your code worked fine for me with VS 2015.

Andy
Last edited on
Well on mine, it's just creating a file like this : ".txt".
I think it doesn't regist the Name..

That sounds suspiciously like the code you're actually using differs from the code you supplied here in a significant way. Specifically, it sounds as if you're doing an input operation prior to getline that leaves a newline in the input stream which getline then encounters and interprets as an empty line.

Please verify the code that you post on these forums reflects the issue in the actual code you're using.
Topic archived. No new replies allowed.