getline不会从变量中读取字符串。 (C ++)

时间:2012-01-18 15:33:16

标签: c++ string file-io getline

我正在尝试使变量存储一个书面问题,然后将其写入文件,然而,该字符串不会被getline读取,当我尝试将其写入文件时,它只会写入任何内容。 / p>

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

using namespace std;

void addquestiontofile(){
    ofstream myfile;
    // Open file to be written to.
    myfile.open("quesitons.txt",ios::ate | ios::app);

    string newquestion;
    cout << "insert new question:  \n";
    getline(cin, newquestion); // This is the problem line

    if(myfile.is_open())
    {
        myfile << newquestion;
    }
}

0 个答案:

没有答案