-2

These are my two classes that hold my data: Base Class holding basic person data:

#ifndef H_personType
#define H_personType

#include <string>



using namespace std;

class person
{
public:

    //get functions
    string getName() const;
    string getId() const;
    string getAge() const;
    string getGender() const;
    string getPhone() const;
    string getEmPhone() const;
    string getAddress() const;
    string getEmail() const;
    string getEmContact() const;

    //set functions
    void setName(string, string, string);
    void setId(string);
    void setAge(string);
    void setGender(string);
    void setPhone(string);
    void setEmPhone(string);
    void setAddress(string, string);
    void setEmail(string);
    void setEmContact(string, string);

    //constructors
    person();
    person(string, string, string,
        string, string, string, string, 
        string, string, string, 
        string, string, string,
        string, string);


private:
    string firstName;
    string lastName;
    string middleName;
    string id;
    string age;
    string phone;
    string address;
    string streetNumber;
    string streetName;
    string gender;
    string email;
    string emergency_phone;
    string emergencyFirstName;
    string emergencyLastName;
    string emergency_contact;
};

Derived Class student that holds member functions to modify data and private members specific for a student.

#ifndef H_studentType
#define H_studentType

#include "personType.h"
#include <vector>

using namespace std;

class student: public person
{
public:

    //void sortRecords();
    friend int checkSize();
    void addRecords();
    void input(istream&, int);
    void print(ostream&) const;
    void printRecords(ostream&);
    void loadData();
    void displayOnScreen();
    void searchID(string, int);
    student();
    student(string, string, string,
        string, string, string, string, string, string,
        string, string, string, string, string, string,
        string, string, string, string);

protected:
    vector<string> tempVec[11];

private:

    string gpa;
    string hobbies;
    string major;
    string honors;
    vector<student> students;
};



#endif

Those two classes are what hold my data. I use an input function to read a simple text input file that holds rows of data for a student, think name, age etc.. the input function fills the vector of type student.

Input function:

void student::input(istream& inF, int size)
{
    //local variables
    student stud;
    string first, middle, last, addressNum, addressStreet, 
        phone, gender, email, emContactFirst, emContactLast,
        ph, emPhone,ID, age;

    while (!inF.eof())
    {
        for (int index = 0; index < size; index++){
            inF >> first >> last >> middle;
            stud.setName(first, last, middle);
            inF >> ID;
            stud.setId(ID);
            inF >> age;
            stud.setAge(age);
            inF >> phone;
            stud.setPhone(phone);
            inF >> addressNum >> addressStreet;
            stud.setAddress(addressNum, addressStreet);
            inF >> gender;
            stud.setGender(gender);
            inF >> email;
            stud.setEmail(email);
            inF >> emPhone;
            stud.setEmPhone(emPhone);
            inF >> emContactFirst >> emContactLast;
            stud.setEmContact(emContactFirst, emContactLast);
            inF >> stud.gpa >> stud.hobbies >> stud.major;

            students.push_back(stud);
        }
    }
}

After this all my data from my input file is stored in the vector of type student, basically a vector of classes that hold my data. In order to perform manipulation on this data (search by studentID, delete record by studentID, modify record by student ID) I want to load it from the vector of classes into an array of string vectors. This is where my mind is melting and I've been stuck for a long time. Here is my load data function and displayOnScreen that simply won't cooperate. The display on screen function is simply for debugging purposes so I can see my filled array printed out.

void student::loadData()
{

        int i = 0;
        for (vector<student>::iterator it = students.begin(); it != students.end(); it++)
        {
            for (i = 0; i < 11; i++)
            {

            cout << "ID = " << it->getId();
            tempVec[i].push_back(it->getName());
            tempVec[i].push_back(it->getId());
            tempVec[i].push_back(it->getAge());
            tempVec[i].push_back(it->getPhone());
            tempVec[i].push_back(it->getAddress());
            tempVec[i].push_back(it->getEmail());
            tempVec[i].push_back(it->getEmPhone());
            tempVec[i].push_back(it->getEmContact());
            tempVec[i].push_back(it->gpa);
            tempVec[i].push_back(it->honors);
            tempVec[i].push_back(it->major);
            }
            if (i = 10)
                break;
        }

}

void student::displayOnScreen()
{
        for (vector<string>::iterator it = tempVec[1].begin(); it != tempVec[1].end(); it++){
            for (int u = 0; u < 11; u++)
            {
                cout << tempVec[1].at(u) << " ";
            }
            cout << endl;
        }

}

Please, I know this is a huge question and any assistance is much appreciated.

kensai01
  • 65
  • 1
  • 3
  • 9
  • 1
    _"I know this is a huge question"_ There's no concrete question at all, or any explanation what actually goes wrong. I'd recommend you post a [MCVE], to get a concise answer. – πάντα ῥεῖ Nov 07 '15 at 18:02
  • `vector tempVec[11];` WHAT?!? An array of vectors of strings? I have never, ever, seen that before. – Bo Persson Nov 07 '15 at 18:23
  • @BoPersson I wish I knew a better way to do it. I don't think I should have used vectors to begin with. I feel like I coded myself into a corner with this project. – kensai01 Nov 07 '15 at 20:28

1 Answers1

1

This is an anti-pattern

while (!inF.eof())
{
}

See: Why is iostream::eof inside a loop condition considered wrong?

This is a bug:

        if (i = 10)  // Notice this sets i to 10 which is true
            break;   // So this breaks the first time.
                     // Use  == for testing (notice two of them)

You can stop this bug by making sure you fix all your warnings. Compile your code with all warnings enabled and ask the compiler to treat warnings as errors (as they are logical errors in your code).

If you want to display a vector of student it is easier to do this:

 for(auto const& s: students)
 {
    std::cout << s << std::endl;
 }

Then define the output operator for student

 std::ostream& operator<<(std::ostream& str, student const& s)
 {
     str << s. getName() << ... etc ...;
     return str;
 }

You should probably also define the input operator for student that way you can localize the reading of a student to the operator>>

PS: I f you want a code review, but only once it is working.

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562