将数据保存到文件c ++中

时间:2014-10-28 05:59:52

标签: c++ visual-c++

我正在努力做到这一点,它就像一项调查,人们必须回答这5个问题,他们的答案(数据)必须保存到file.txt然后程序必须读取数据。然后,让我们说用户回答A的第一个问题,它必须说20个调查回答A(20/100)的人数与其他5个问题相同。但我很失落,我不知道如何让程序计算有多少人参加调查,以及如何存储他们回答文件。请帮助!

int questions = 1;
char answer;

for (int i = 0; i < questions; i++){

    cout << "\t1 - What kind of phone do you prefer? " << endl;
    cout << "\n";
    cout << "\ta) Iphone    b) Android  c) Windows  d) Other" << endl;
    cout << "\n";
    cout << "\tAnswer: ";
    cin >> answer;
    cout << "\n";

    cout << "\t2 - What is your favorite flavor? " << endl;
    cout << "\n";
    cout << "\ta) Chocolate b) Vanilla  c) Strawberry   d) Other" << endl;
    cout << "\n";
    cout << "\tAnswer: ";
    cin >> answer;
    cout << "\n";

    cout << "\t3 - What is your favorite sport? " << endl;
    cout << "\n";
    cout << "\ta) Baskteball    b) Baseball c) Hokey    d) Football" << endl;
    cout << "\n";
    cout << "\tAnswer: ";
    cin >> answer;
    cout << "\n";

    cout << "\t4 - What is your favorite animal? " << endl;
    cout << "\n";
    cout << "\ta) Cat       b) Dog      c) Bird     d) Other" << endl;
    cout << "\n";
    cout << "\tAnswer: ";
    cin >> answer;
    cout << "\n";

    cout << "\t5 - What is your favorite season of the year? " << endl;
    cout << "\n";
    cout << "\ta) Summer    b) Winter   c) Spring   d) Autumn" << endl;
    cout << "\n";
    cout << "\tAnswer: ";
    cin >> answer;
    cout << "\n";
}



system("pause");
return 0;

}

1 个答案:

答案 0 :(得分:0)

Google搜索&#34; c ++写入文件&#34;。首先点击:http://www.cplusplus.com/doc/tutorial/files/

这应该回答你的所有问题。

简短说明:

您可以像cout一样处理文件。打开它,然后打开文件输出流。

那里也有读书。

但是:看起来你并没有真正想到一个解决方案。用自然语言写下整个过程的逐步描述。请注意应该在文件中的哪个位置写入,以便您以后可以阅读它。这也可能有用,看看任务更清晰。

你注意到了这些挫折? 你得到它们是因为你可以用5secs谷歌搜索来解决这个问题,你的问题描述非常清楚,你求助(我们在这里帮助)。

我建议你阅读:https://stackoverflow.com/help/how-to-ask