outfile.open() problomes

hey, im trying to write a program that will create a outfile and open it but the name of the file will be detimermind by a variable in the program. is there any way to do this? i want something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// variables
string lastname;

int main(){
cout << "Enter your lastname:\n";
cin >> lastname;

outfile.open(lastname".txt"); /*  <-- i need to get this to work to 
                                         finish my project any help would be
                                         thanked.*/
You need to add a '+' in between lastname and ".txt".
Still need help!!!


thanks firedraco, i love how trivial it always turn out to be. im going to try that now

nope --> ive tryed adding a + in between them do i have to do anything else. it now looks like

outfile.open(lastname+".txt");

it builds with no issues but then it wont open the file.
Last edited on
Odd. That shouldn't compile.

std::fstream::open() takes (for some reason) a C string as its first parameter, and you're trying to pass an std::string.
Wrap the concatenation with parentheses and make a call to std::string::c_str(). It should look like (a+b).c_str().
Topic archived. No new replies allowed.