【问题标题】:C++ Reading from file into class object array to print 25 linesC++ 从文件读取到类对象数组以打印 25 行
【发布时间】:2017-03-06 02:28:44
【问题描述】:

我正在为我的 c++ 课做这个作业,我已经做了几个小时了,但没有取得任何进展。我已经对我的问题进行了大量搜索,但没有任何效果。我来这里是为了解决我的问题。

我有一个名为“hw2data.txt”的文件,其中包含 25 行信息,我现在正试图将它简单地输出到控制台,但我的作业说我需要使用一组 Student(我的班级名称) 保存25个对象 在main函数中,程序从数据文件中读取数据,调用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;
}

我已经尝试了多种方法来让它工作,我得到的主要错误是 “二进制'>>':没有找到采用'重载函数'类型的右手操作数的运算符(或没有可接受的转换)” 请帮忙

【问题讨论】:

  • 这里有一个可能有帮助的快速阅读:Input/Output operators overloading in C++
  • 您声称您尝试了多种方法来使其工作,但您在此处显示的是仅打开文件的代码。你具体尝试了什么?我们可以提供帮助,但我们无法为您完成这项作业。
  • 我已经尝试了多种方法,我并不是要求为我完成它,我在许多这些堆栈线程上看到了相同的响应。我曾尝试使用诸如“inFile >> students.getID;”之类的代码来显示文件的第一部分,也尝试使用 getline 但我不是很熟悉。我正在寻找的是有人解释使用类对象数组将此文件打印到控制台所需的内容。我以前从未这样做过,我希望有人通过解释或展示示例来帮助我理解。

标签: c++ arrays class object


【解决方案1】:

正如您在评论中提到的那样,使用inFile &gt;&gt; students.getID; 不会做任何事情。看看getID 函数:int Student::getID()。它除了返回一个整数之外什么都不做(更不用说有任何重载的运算符,稍后您将了解)。

如果要将信息存储到 Student 对象中,您有两种选择,使用构造函数或使用 set 函数。在尝试填充整个数组之前,尝试将 1 个学生读入单个学生对象可能是一个好习惯。

这是一个可以帮助你的例子:

#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

【讨论】:

  • 这是一个很好的例子,我不知道如何在这个评论中添加代码,但我得到了与你的例子类似的东西,但现在无法将 txt 文件中的 5 个分数放入我在 student.cpp 中创建的名为 scores[5] 的类数组
  • 我已经更新了我的示例以包含一个数组。这不是最有效的做事方式,但更容易遵循。
  • 在你更新它之前我刚刚弄明白了,我现在可以使用类对象将每一块打印到控制台,这是我的工作 cout 代码code cout
【解决方案2】:

好的,要将文件的内容输出到屏幕上,您可以执行以下操作:

#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];

您还必须考虑到您的文件包含应该跳过的 2 行。

拥有所有数据后,您应该很容易构造Student 对象。

了解std::arraystd::stringstd::istringstream

【讨论】:

  • 您可能应该grab a good C++ book。您已经获得了提取所有变量的代码,只需构造 Student 对象,调用适当的构造函数,然后调用一个 setter。
  • 好的,所以我现在将它打印到我的控制台,但我想要做的是将每一行的每个部分填充到类的正确变量中,我尝试执行 `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]; ` 但这会出现错误,我不确定为什么,它应该只读取文件并将每个部分放入这些变量中吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2014-04-06
  • 2017-09-08
相关资源
最近更新 更多