在下面的代码中,标题和正文在一个单词后被删除。我认为字符串可以容纳几个字?有人可以解决这个问题。感谢
#include <fstream.h>
#include <iostream.h>
#include <string>
using namespace std;
string data, newtitle, body;
ofstream outfile;
int main()
{
cout << "enter title of note: ";
cin >> newtitle;
cout << "enter body of note: ";
cin >> body;
data = newtitle + ".dat";
outfile.open(data.c_str(), ios::out);
outfile << body << endl;
outfile.close();
system("pause");
}
答案 0 :(得分:1)
cin.operator>>
分隔任何空格(包括空格)。您可以使用getline
获取整行输入:
cout << "enter title of note: ";
getline(cin, newtitle);
cout << "enter body of note: ";
getline(cin, body);