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



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

我正在使用一个函数将包含学生信息的文件(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 . . .

我认为问题发生在您打开文件的方式上

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

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

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

请参阅此链接,了解如何检查是否成功从文件读取

最新更新