2017-03-06 52 views
-3

我想爲我的C++類做這個作業,並且我一直在爲它工作幾個小時,而且沒有取得任何進展。我在我的問題上做了一堆搜索,沒有任何真正的工作。我來這裏希望解決我的問題。C++從文件讀入類對象數組打印25行

我有一個名爲「hw2data.txt」的文件,有25行信息,我現在試圖簡單地將它輸出到控制檯,但是我的作業說我需要使用Student數組來完成它(我的班級名稱)保存25個對象在主函數中,程序從數據文件中讀取並調用Student類的成員函數來設置成員變量並輸出結果。

我希望能夠在進入函數並將其添加到結果中之前完全輸出文件中的文本。

TXT文件

10001 Alice Brown  89 85 92 99 90 
10004 Bob Bush   85 76 83 79 83 
10010 Carl Capra  65 57 73 68 76 
10012 David Lieberman 76 68 79 81 58 
10034 John Menchin  100 89 95 93 88 
10056 George Smith  86 78 90 80 95 
10062 Elaine Sanders 85 79 90 80 95 
10078 Jack Cunningham 72 78 82 97 84 
10090 Susie Brown  68 85 80 84 83 
10099 Marvella Garcia 86 92 88 97 98 
10120 Tony Peterson  85 84 83 90 76 
10129 John Jones  75 75 80 84 80 
10131 Mary Evans  60 72 89 86 65 
10146 Nancy Drew  78 80 75 90 85 
10152 Lola Zapeta  89 81 98 89 97 
10155 Duckey Donald  82 60 73 78 55 
10163 Goof Goofy  89 78 75 89 56 
10168 Brave Balto  100 98 93 89 92 
10178 Snow Smitn  93 76 54 83 80 
10184 Alice Wonderful 86 79 87 78 67 
10192 Samina Akthar  85 62 78 45 60 
10207 Simba Green  50 45 35 60 20 
10211 Donald Egger  76 80 83 68 81 
10216 Brown Deer  86 87 89 79 75 
10245 Johny Jackson  96 85 91 83 79 

學生類文件

#include <iostream> 
#include <fstream> 
#include <string> 
#include "Student.h" 
using namespace std; 

void Student::setID(int tID) 
{ 
    ID = tID; 
} 

void Student::setFName(string f) 
{ 
    firstName = f; 
} 

void Student::setLName(string l) 
{ 
    lastName = l; 
} 

void Student::setScores() 
{ 

} 

int Student::getID() 
{ 
    return ID; 
} 

string Student::getFName() 
{ 
    return firstName; 
} 

string Student::getLName() 
{ 
    return lastName; 
} 

int Student::getWeightedTotal() 
{ 
    return 0; 
} 

int Student::getGrade() 
{ 
    return 0; 
} 

void Student::printStudent() 
{ 
} 

Student::Student() 
{ 
    ID = 0; 
    firstName = ""; 
    lastName = ""; 

} 

Student::Student(int tID, string f, string l) 
{ 
    setID(tID); 
    setFName(f); 
    setLName(l); 


} 

Main.cpp的文件中獲取此的

#include <iostream> 
#include <fstream> 
#include <string> 
#include "Student.h" 
using namespace std; 

int main() { 

    Student students; 
    Student sa[25]; 
    ifstream inFile; 
    inFile.open("hw2data.txt"); 






    system("pause"); 
    return 0; 
} 

我試過多種方式來工作,主要錯誤我得到的是 「binary」>>':找不到操作符找到類型爲「過載」的右側操作數ED-功能」(或沒有可接受的轉換)」 請幫

+0

這裏有一個快速閱讀,可以幫助:輸入/輸出操作人員用C重載++] (https://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm) –

+1

你聲稱你試過*多種方式來獲得這個工作*但你在這裏展示的是隻打開一個文件的代碼。你有什麼特別的嘗試?我們可以幫忙,但我們不能爲你做這個功課。 – mpiatek

+0

我已經嘗試了多種方式,我並沒有要求它爲我完成,我在這些堆棧線程中看到了相同的響應。我曾嘗試使用諸如「inFile >> students.getID;」之類的代碼來顯示文件的第一部分,並嘗試使用getline,但我並不十分熟悉它。我在找的是有人解釋使用類對象數組將此文件打印到控制檯所需的內容。我從來沒有做過,我希望有人通過解釋或展示一個例子來幫助我理解。 – saviro

回答

0

使用inFile >> students.getID;,正如您在您的評論中提到,什麼都不會做。看看getID函數:int Student::getID()。除了返回一個整數之外,它不會做任何事情(更不用說有任何重載的操作符,您將在後面瞭解這些操作符)。

如果要將信息存儲到Student對象中,您有兩個選擇,使用構造函數或使用set函數。在試圖填充整個數組之前,嘗試將1名學生讀入單個Student對象可能是一種很好的做法。

下面是一個例子,可以幫助你:

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

class Cat 
{ 
    int weight; 
    string favoriteFoods [3]; 
public: 
    void set_weight (int); 
    void set_favoriteFoods (string); 
}; 

void Cat::set_weight (int w) 
{ 
    weight = w; 
} 

void Cat::set_favoriteFoods (string ff[3]) 
{ 
    favoriteFoods = ff; 
} 

int main() 
{ 
    int catWeight; 
    string catFavoriteFoods [3]; 

    ifstream infile; 
    infile.open ("kittyInfo.txt"); 

    if(infile.is_open()) 
    { 
     infile >> catWeight; 
     infile >> catFavoriteFoods[0]; 
     infile >> catFavoriteFoods[1]; 
     infile >> catFavoriteFoods[2]; 
    } 

    Cat kitty; 
    kitty.set_weight (catWeight); 
    kitty.set_favoriteFood(catFavoriteFoods); 

    infile.close(); 

    return 0; 
} 

假設kittyInfo.txt看起來是這樣的:

15  fish milk fear 
+0

這是一個很好的例子,我不知道如何把代碼在此發表評論,但我有類似的例子的東西,但我現在不得不用得到5分從txt文件進級陣列麻煩我創建了一個名爲得分[ 5]在student.cpp – saviro

+0

我已更新我的示例以包含一個數組。這不是最有效的做事方式,但更容易遵循。 –

+0

我剛剛在更新它之前已經想通了,現在我可以使用類對象將每件作品打印到控制檯,這裏是我的工作代碼 'code' \t cout << students.getID()<< ENDL; \t COUT << students.getFName()<< ENDL; \t cout << students.getLName()<< endl; (int i = 0; i <5; i ++) \t \t \t cout << score [i] << endl; – saviro

0

好了,到輸​​出文件的內容到屏幕上,你可以做這樣的事情:

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

int main() { 
    std::ifstream inFile; 
    inFile.open("hw2data.txt"); 

    for (std::string line; std::getline(inFile, line);) { 
     std::cout << line << '\n'; 
    } 

    return 0; 
} 

這裏我們稱之爲std::getline在循環以從文件中獲取一行文本到類型std::string的變量line

在你的例子中,當你有你的線,你需要適當地解析它以獲得所有的值。您可以利用std::istringstream所以像下面的代碼需要在適當的地方添加:

#include <array> 
#include <sstream> 

std::istringstream stream(line); 
int id; 
std::array<int, 5> scores; 
std::string first_name, last_name; 

stream >> id >> first_name >> last_name >> scores[0] >> scores[1] >> scores[2] >> scores[3] >> scores[4]; 

你還必須考慮到,你的文件包含兩行應跳過戶頭。

擁有所有數據後,您應該很容易構建Student對象。

閱讀std::arraystd::stringstd::istringstream

+0

你應該[抓住一個很好的C++書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。你已經得到了提取所有變量的代碼,只需構造Student對象調用合適的構造函數,然後調用一個setter。 – mpiatek

+0

好了,所以我把它打印到我的控制檯了,但我想要做的是有填充到類的權變量各行的一部分,我試圖做 ' INFILE >> SAA [25] .setID> > SAA [25] .setFName >> SAA [25] .setLName >> SAA [25] .setScores [0] >> SAA [25] .setScores [1] >> SAA [25] .setScores [2] >> saa [25] .setScores [3] >> saa [25] .setScores [4]; ' 但出來的錯誤,我真的不知道爲什麼,它應該只是讀取文件,並把它們放在那些變量,如果它不是? – saviro