cout << "Enter your full name";
char* name ;
cin >> name;
if( _mkdir("c:\\names") == 0 ) {
cout << "directory successfully created";
} else {
cout << "there was a problem creating a directory";
}
Now i want to create a file ( a .txt file ) in the directory names with the same name as the name of the user. I mean the name which the user entered during cin >> name;.
How can i do this ?
ofstream writeName( "c:/names/????); —-> PROBLEM
Use
std::stringinstead ofchar*. As it is your code has Undefined Behavior. Usestd::getlineinstead of>>. With>>only the first whitespace-separated “word” is input. Then, compose the full path in astd::string. The standard string class supports concatenation, so this should be easy.Say, if that string is
path,Cheers & hth.,