我只能在文件中输入一个单词

时间:2017-04-20 20:29:49

标签: c++ fstream

就像我的标题所说,我遇到文件输入问题。

这是我的代码:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void CreateNewFile(const string FileName) {
    ofstream NewFile;
    string FileNamePlaceholder = FileName + ".txt";
    NewFile.open(FileNamePlaceholder);
    cout << "What would you like to write to the file? ";
    string InputForFile;
    cin >> InputForFile;
    NewFile << InputForFile;
    NewFile.close();
    cout << "You will now be able to view the file in your computer.\n";
}
void Start() {
    char NewFile;
    cout << "Would you like to create a new file? [Y/N] ";
    cin >> NewFile;
    if (NewFile == 'y' || NewFile == 'Y') {
        cout << "What would you like the new file to be named? ";
        string NewFileName;
        cin >> NewFileName;
        CreateNewFile(NewFileName);
    }
    else if (NewFile == 'n' || NewFile == 'N') {
        cout << "Ok, bye.\n";
    }
    else {
        cout << "You did not type y, Y, n, or N!\n";
    }
}

int main(void) {
    Start();
    return 0;
}

出于某种原因,我一次只能输入一个单词到文件中。如果这是一个明显的答案,我很抱歉。任何帮助将不胜感激。

0 个答案:

没有答案