我有一個結構:如何要求輸入一次初始化多個結構
struct Student {
string name;
int CWID;
float GPA;
};
現在,我有5個學生的數組:
Student* s = new Student[5];
現在我要對它們進行初始化使用控制檯輸入以這種形式:
John 5.5
Mike 5012432 3.6
Ryan 5013521 3.3
Mary 5012784 4.0
Harry 5012695 3.1
我的瑣碎的邏輯是:
for (int i = 0; i < 5; i++) {
cout << "\n\nPlease enter Student " << (i + 1) << "'s details (Name, CWID, GPA): ";
cin >> s[i].name >> s[i].CWID >> s[i].GPA;
}
與其單獨詢問5個輸入,我怎樣才能一次全部使用它們?
除了提示之外,一次不應該有任何區別。順便說一句,如果你知道數組的大小,使用一個數組(如果可以的話,可以使用'std :: array')。如果你不這樣做,請使用'std :: vector'。不要使用'new []'。 – chris 2015-04-02 20:21:48
從文件中讀取輸入時如何? – 2015-04-02 20:23:41
如何在從文件讀取數據時搜索StackOverflow以獲取答案?試試這個:[「stackoverflow C++ read file structure」](https://www.google.com/search?q=stackoverflow+c%2B%2B+read+file+structure&ie=utf-8&oe=utf-8) – 2015-04-02 21:16:18