I'm teaching myself c++, and after searching around on the forums, I couldn't find an answer that helped me with this specific problem.

The idea is to collect the first name, last name, and SSN of a customer, and create a file using the first three letters of the first name adn last name, and the last four of the SSN to create a file name.

Sample input
Sam
Bronikowsky
5555

The file name would read sambro5555

heres the section of code i've written.

void new_customer()
{
    string f_name, l_name, name_file;
    string ssn;
    char yesno;
    cout<<"You have selected 'New Customer'"<<endl;
    getch();
    do
    {
        system("cls");
        cout<<"First Name: ";
        cin>>f_name;
        cout<<"Last Name: ";
        cin>>l_name;
        cout<<"Last Four Didgets of Social Security: ";
        cin>>ssn;
        system("cls");
        cout<<l_name<<", "<<f_name<<endl<<ssn<<endl;
        cout<<"Is this correct? Y / N"<<endl;
        yesno = getch();
    }while (yesno != 121);

    system("cls");
    name_file = f_name.substr(0, 3) + l_name.substr(0, 3) + ssn;
    cout<<name_file;

    ofstream out_to_file (name_file);
    out_to_file<<f_name<<" "<<l_name;
}

so how can I get the string variables to read out as a custom file name?

If you paid attention to the error message generated by this code, you'd have an idea that the ofstream cannot take a string type as the filename parameter. You must use a C-style, character array based, null terminated string for filenames.

But, there's a way out. Look up the c_str( ) method of strings.

ok, sweet. now it works. added

char * cfile_name;

and

cfile_name = new char [buffer_file_name.size()+1];
strcpy (cfile_name, buffer_file_name.c_str());

thanks for the help.

This article has been dead for over six months. Start a new discussion instead.