如何在c ++中将字符串数组数据写入输出文件

时间:2018-01-10 03:20:15

标签: c++ arrays

所以最近我一直在开发一个程序,询问用户一些问题并询问他们的回复,然后将其存储到一个数组中,并将其写入输出文件,尽管我没有得到这些信息用户输入但代码为0x4080c0。有人可以帮助我,这也是我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string q1 = "Distance Flown:";
string q2 = "Total Fuel Used:";
string q3 = "Total Flight Time:";
string q4 = "Number Of Passengers:";
string q5 = "Total Profit:";
string q6 = "Any futher comments goes here:";

string logAwensers[6];

int main()
{
cout << "Please enter your pilot log here."<< endl;
cout << q1 << endl;
getline(cin, logAwensers[0]);
cout << q2 << endl;
getline(cin, logAwensers[1]);
cout << q3 << endl;
getline(cin, logAwensers[2]);
cout << q4 << endl;
getline(cin, logAwensers[3]);
cout << q5 << endl;
getline(cin, logAwensers[4]);
cout << q6 << endl;
getline(cin, logAwensers[5]);

cout << logAwensers << endl;
ofstream writer("PilotLog.txt", ios::app);

if(! writer)
{
    cout << "Error writing file" << endl;
}

    return -1;
}
writer << logAwensers << endl;
writer.close();
return 0;
}

感谢。

1 个答案:

答案 0 :(得分:3)

而不是:

cout << logAwensers << endl;

尝试类似:

for (const auto &a : logAwensers)
{
    cout << a << endl;
}