0

Sorry if this is a dumb question, I'm a noob.

I'm using a function to read a file(readStudentRecord), containing student information, into an array of objects then another function (displayAllStudents) to print out this information. The problem is my program is just printing garbage data, so I think I'm going about this the wrong way.

I'm new to OOP and I'm still fuzzy on how everything is suppose to interact with classes and different specification/implementation files, so any help would be greatly appreciated.

Edit:

After some modifications, I'm now outputting 0's instead of garbage. I believe the problem is my ID variable for some reason my file is not reading into it which I don't understand. Resulting in the output of 0's. Otherwise my file is reading successfully.

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

6
  • 2
    -858993460 translates to hexadecimal as CCCCCCCC, and that is WAY to regular to be dumb luck, especially not 150 times in a row. The program is trying to tell you something. Commented Jul 15, 2019 at 20:02
  • I see nothing in readStudentRecord that ensures ANY of the file reads succeeded or that the file was even opened. Commented Jul 15, 2019 at 20:06
  • I made sure that the file is openable and ran your code. It runs and outputs sane results. You have a problem because for (int i = 0; i < MAX_NUM; i++) keeps reading past the end of the file, but add some checks to make sure the file is being opened, put the input file in the right spot, and this bug should be cleared. Commented Jul 15, 2019 at 20:27
  • 1
    Always check when you open a file that it succeeds. I'd bet a penny to a pound that you are failing to open your file. When you've confirmed that we can talk about why the file open might be failing. Commented Jul 15, 2019 at 21:34
  • Again? stackoverflow.com/questions/57007825/… . Code improved drastically. But reading the file seems to be still a problem. Please check always the state of your ifstream. Any fail bit set? You read 25 lines. What if there are less lines? Do never use C-style arrays! Commented Jul 16, 2019 at 5:33

1 Answer 1

0

I think the problem occurs with how you open the file

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

This is how you open a text file. If point.dat is binary then your getline() will fail and return empty line. Try creating manually a new point.txt file in notepad, and see what you get.

You should also handle a case when number of records in file is less than MAX_NUM, you should count how many record actually read and print only the number of records read.

See this link on how to check if read from file succeeded

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.