String Concatenation for File Ouput

Hi all, this is my first post here but I've been lurking for awhile. I've not had a reason to post before now that I couldn't find an answer to already. You all are awesome.

Anyways, on to the question. I'm trying to concatenate a string from user input to be used as the output file name. The user inputs the month and account number and the file name should be in the form of Sep00000000.out. I'd like to direct the file name to a folder on the desktop.

What I have now would isn't working for me and while I'm sure I'm making a stupid mistake I can't quite figure it out.

1
2
3
4
5
6
7
8
9
10
    ofstream billOut;
    string outputFileName;

    outputFileName[0] = month[0];
    outputFileName[1] = month[1];
    outputFileName[2] = month[2];
    outputFileName = outputFileName[0]+outputFileName[1]+outputFileName[2];
    outputFileName = "C:\\docume~1\\onreyv\\desktop\\project1" + outputFileName + account + ".out";

    billOut.open(outputFileName.c_str());


I appreciate your help.
You did not show what is the type of variables month and account.
In any case I think that your result string is calculated the following way

1
2
3
4
string outputFileName = "C:\\docume~1\\onreyv\\desktop\\project1\\";
ouputFileName += month;
ouputFileName += account;
ouputFileName += ".out";




Last edited on
I apologize, month and account are both string variables.
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum { MONTH_SZ = 3, ACCOUNT_SZ = 8 } ;
static const std::string path2dir = "the actual path to the desktop directory" ;
static const std::string extension = ".txt" ;
const char separator = '/' ;

std::string month, account ;
std::cin >> month >> account ;

// http://www.cplusplus.com/reference/string/string/substr/
if( month.size() > MONTH_SZ ) month = month.substr(0,3) ;
else if( month.size() < MONTH_SZ ) { /* error */ }
// TO DO: validate month

if( account.size() < ACCOUNT_SZ )
      account = std::string( ACCOUNT_SZ - account.size(), '0' ) + account ;
else if( account.size() > ACCOUNT_SZ ) { /* error */ }
// TO DO: validate account

const std::string file_name = path2dir + separator + month + account + extension ;

Topic archived. No new replies allowed.