带输出的C++问题
我需要用户键入他想要写入数据的文件。 示例:您还想编写什么文本文件? 他键入to:example,数据将进入example.txt文件,它也将包含Hello。问题是它不能使用字符串accname和combine.txt将数据发送到文本文件。很难解释,希望你能理解:D带输出的C++问题,c++,string,random,output,C++,String,Random,Output,我需要用户键入他想要写入数据的文件。 示例:您还想编写什么文本文件? 他键入to:example,数据将进入example.txt文件,它也将包含Hello。问题是它不能使用字符串accname和combine.txt将数据发送到文本文件。很难解释,希望你能理解:D int Transfersum; string accname; cout << "Type the account name you want to send too" << endl; cin >&
int Transfersum;
string accname;
cout << "Type the account name you want to send too" << endl;
cin >> accname;
ofstream mfile;
mfile.open ( accname ".txt");
mfile << Transfersum;
mfile.close();
您应该能够只使用带+的字符串连接
如果你不是C++ 11,那么你可能需要一个C风格的字符串。
mfile.open ( (accname + ".txt").c_str());
您必须连接accname+.txt,才能将其用作mfile.open调用的参数表达式。字符串连接在任何好书的前两章中。mfile.open accname+.txt;要连接字符串设置此错误:73 32 C:\Users\Winston\Desktop\C++\main.cpp[error]调用'std::basic_of stream::openstd::basic_string'@user3748896没有匹配的函数,请参阅更新。谢谢:D我能做些什么来奖励你?@user3748896,如果这个答案解决了你的问题,请将其标记为已接受的答案。我不需要等待5分钟,但到时候我会做的。
mfile.open ( (accname + ".txt").c_str());