2014-10-28 142 views
-3

我很難做到這一點,它就像一個調查,人們必須回答這5個問題,他們的答案(數據)必須保存到一個file.txt然後程序必須讀取數據。然後,讓我們說用戶回答A的第一個問題,就是說,在接受調查答案A的人數(20/100)中,有20個與其他5個問題相同。但即時通訊失去了我不知道如何使程序計數有多少人蔘與調查,以及如何將它們存儲到一個文件中。請幫忙!將數據保存到文件中C++

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; 

}

+0

所以ü要一個人保存到一個文件?然後計算這些文件? – Quest 2014-10-28 06:03:25

回答

0

谷歌搜索 「C++寫入文件」。第一次打:http://www.cplusplus.com/doc/tutorial/files/

這應該回答你所有的問題。

簡述:

您可以像cout一樣處理文件。打開它,然後寫入文件輸出流。

閱讀也在那裏描述。

但是:看起來你並沒有真正的解決方案。用自然語言寫下整個過程的一步一步描述。注意什麼應該寫在文件的哪裏,以便以後可以閱讀。這也許有用,看到任務有點清晰。

您注意到了降價? 你得到了他們,因爲你可以用5秒的谷歌搜索來解決這個問題,你的問題描述很不清楚,你求助(我們來幫忙)。

我建議你閱讀本:https://stackoverflow.com/help/how-to-ask