2017-03-06 86 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