【问题标题】:Problem reading data file into an array of objects将数据文件读入对象数组时出现问题
【发布时间】:2019-07-15 19:41:10
【问题描述】:

对不起,如果这是一个愚蠢的问题,我是菜鸟。

我正在使用函数将包含学生信息的文件 (readStudentRecord) 读取到对象数组中,然后使用另一个函数 (displayAllStudents) 打印出此信息。问题是我的程序只是打印垃圾数据,所以我认为我的做法是错误的。

我是 OOP 的新手,我仍然不清楚一切应该如何与类和不同的规范/实现文件进行交互,因此非常感谢任何帮助。

编辑:

经过一些修改,我现在输出的是 0 而不是垃圾。我相信问题出在我的 ID 变量上,出于某种原因,我的文件没有读入我不明白的内容。导致输出0。否则我的文件读取成功。

This is my input file and desired output

     ID   CLA   OLA   Quiz   Homework   Exam   Bonus   Total   FinalGrade   
c088801    10    15      4         15     56       5 
c088802     9    12      2         11     46       2 
c088803     8    10      3         12     50       1
c088804     5     5      3         10     53       3
c088805     3    11      1         10     45       0 
c088806     8    14      2         11     40      -1  
c088807     4    12      2         12     48      -2
c088808    10    10      3         11     36       0
c088809     8     8      3         11     39       0
c088810     6     9      4          9     47       3
c088811     8     7      3         13     41       3
c088812     4    11      3         11     37       1
c088813     9    15      2          8     50       2
c088814     8    12      2         10     48       4
c088815     6     8      1          7     45       1
c088816     7     7      2          6     51       2
c088817     8     9      2         12     38       2 
Student.h and Roster.h file

#ifndef STUDENT_H
#define STUDENT_H

#include <string>
#include <stdexcept>


class Student
{
    public:

        enum ScoreType {CLA, OLA, QUIZ, HOMEWORK, EXAM, BONUS};
        static const int CATEGORY_NUM = BONUS - CLA + 1;

        Student(void);

        //Accessor & mutator of m_id 
        std::string getID(void) const;
        void setID(std::string) ;

                //Accessor and mutator of m_score
        void changeScore(const ScoreType, const int);
        int  getScore(const ScoreType) const;


    private:
        std::string m_id;       // Student ID
        int m_score[CATEGORY_NUM];
};
#endif

#ifndef ROSTER_H
#define ROSTER_H

#include <string>
#include <stdexcept>
#include "Student.h"

class Roster
{
    public:

        Roster(std::string courseName);

        void readStudentRecord();
        void displayAllStudents();

    private:
        static const int MAX_NUM = 25;  //Max student # in class

        std::string m_courseName;       //Name of course
        int m_studentNum;           //Actual student #
        Student m_students[MAX_NUM];    //Array of student objects
};
#endif
Student.cpp file

#include "Student.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Student constructor
Student::Student(void) {

}

//Accesor and Mutator of m_id
string Student::getID(void) const {
    return m_id;
}

void Student::setID(string ID) {
    m_id = ID;
}

//Accessor and Mutator of m_score
int Student::getScore(const ScoreType st) const {
    return m_score[st];
}

void Student::changeScore(const ScoreType st, const int score) {
    m_score[st] = score;
}

Roster.cpp file

#include "Roster.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


//Roster constructor
Roster::Roster(string courseName){
    m_courseName = courseName;
    m_studentNum = 0;
}   


//Function will read from input file and store data into an array of objects.
void Roster::readStudentRecord() {
    Student m_students[MAX_NUM];
    string ID, line;
    int CLA, OLA, Quiz, Homework, Exam, Bonus;



    ifstream inFile;                                //Reads input file
    inFile.open("point.dat");

    getline(inFile, line);                          //Skips first line of file
    int i = 0;

    inFile >> ID >> CLA >> OLA >> Quiz >> Homework >> Exam >> Bonus;
    while (inFile) {
        inFile >> ID >> CLA >> OLA >> Quiz >> Homework >> Exam >> Bonus;
        i++;
    }


    for (int i = 0; i < MAX_NUM; i++) {
        m_students[i].setID(ID);
        m_students[i].changeScore(Student::CLA, CLA);
        m_students[i].changeScore(Student::OLA, OLA);
        m_students[i].changeScore(Student::QUIZ, Quiz);
        m_students[i].changeScore(Student::HOMEWORK, Homework);
        m_students[i].changeScore(Student::EXAM, Exam);
        m_students[i].changeScore(Student::BONUS, Bonus);
        //>> m_students[i].total >> m_students[i].letterGrade;
    }
    cout << endl;
    cout << "file read sucessfully" << endl;
    inFile.close();
}

//Function will display every student's information from roster.
void Roster::displayAllStudents() {
    cout << endl;
    cout << "All student information is given below:" << endl;
    cout << endl;
    cout << "     ID   CLA  OLA  Quiz  Homework  Exam  Bonus Total" << 
        endl;

    for (int i = 0; i < MAX_NUM; i++) {
        cout << m_students[i].getID() << "     " << 
                m_students[i].getScore(Student::CLA) << "   " <<
        m_students[i].getScore(Student::OLA) << "   " << 
                m_students[i].getScore(Student::QUIZ) << "   " <<
        m_students[i].getScore(Student::HOMEWORK) << "   " << 
                m_students[i].getScore(Student::EXAM) << "   " << 
                m_students[i].getScore(Student::BONUS) << "   " << endl;
    }

}
main.cpp

#include "Student.h"
#include "Roster.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    Roster obj("CSCI");

    obj.readStudentRecord();
    obj.displayAllStudents();

    system("pause");
    return 0;
}
My output

file read sucessfully

All student information is given below:

     ID   CLA  OLA  Quiz  Homework  Exam  Bonus Total
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
     0   0   0   0   0   0
Press any key to continue . . .

【问题讨论】:

  • -858993460 转换为十六进制为 CCCCCCCC,这是正常的运气,尤其是连续 150 次。 The program is trying to tell you something.
  • 我在 readStudentRecord 中看不到任何文件可以确保任何文件读取成功或文件甚至已打开。
  • 我确保该文件是可打开的并运行了您的代码。它运行并输出合理的结果。你有一个问题,因为for (int i = 0; i &lt; MAX_NUM; i++) 一直在读取文件末尾,但是添加了一些检查以确保文件正在打开,将输入文件放在正确的位置,这个错误应该被清除。
  • 始终在您打开文件时检查它是否成功。我赌一分钱你没能打开你的文件。当您确认我们可以讨论文件打开失败的原因时。
  • 再来一次? stackoverflow.com/questions/57007825/… 。代码大大改进。但是读取文件似乎仍然是一个问题。请始终检查您的 ifstream 的状态。任何失败位设置?你读了 25 行。如果行数少了怎么办?不要使用 C 风格的数组!

标签: c++ arrays class object io


【解决方案1】:

我认为问题在于您打开文件的方式

`ifstream inFile("point.dat"); `

这是您打开文本文件的方式。如果 point.dat 是二进制的,那么您的 getline() 将失败并返回空行。 尝试在记事本中手动创建一个新的 point.txt 文件,看看会得到什么。

您还应该处理文件中记录数小于 MAX_NUM 的情况, 您应该计算实际读取的记录数并仅打印读取的记录数。

请参阅this link,了解如何检查文件读取是否成功

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    相关资源
    最近更新 更多