将数据文件读入对象数组时出现问题

时间:2019-07-15 19:41:10

标签: c++ arrays class object io

抱歉,这是一个愚蠢的问题,我是菜鸟。

我正在使用一个函数将包含学生信息的文件(readStudentRecord)读入对象数组,然后使用另一个函数(displayAllStudents)打印此信息。问题是我的程序只是打印垃圾数据,所以我想我将以错误的方式进行处理。

我是OOP的新手,我仍然对一切应该与类和不同的规范/实现文件进行交互的方式一无所知,因此将不胜感激。

编辑:

经过一些修改,我现在输出0而不是垃圾。我认为问题是我的ID变量,出于某种原因,我的文件未读入我不理解的文件。结果输出为0。否则我的文件读取成功。

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 . . .

{{1}}

1 个答案:

答案 0 :(得分:0)

我认为问题在于您如何打开文件

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

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

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

有关如何检查从文件读取是否成功的信息,请参见this link