Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3395528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:18:02+00:00 2026-05-18T04:18:02+00:00

I think I am having trouble with binary file io. If I run my

  • 0

I think I am having trouble with binary file io. If I run my program, create some employee objects and then display them everything works fine. If I save the object data and reload the program I get an RTTI exception. It apears to me that my LoadEmployeeData() and Savelist(vector &e) functions work just fine. The exception occurs in my DisplayEmployeeData() function when I try to use typeid.

Just to reiterate, I am getting an RTTI error when using typeid on an object loaded from disk.

//****************header file***********
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <typeinfo>
#include <ctime>
#include <cstdlib>
using namespace std;
class Employee
{
private:
    int employeeID;
    char name[80];
    int SSN;
public:
    Employee();
    Employee(int, char*,int);
    virtual ~Employee();
    virtual void DisplayBaseData();
    //getters
    int GetID();
    char* getName();
    int GetSSN();
    //setters
    void SetID(int);
    void SetName(char*);
    void SetSSN(int);
};//end Employee class

class Salary : public Employee
{
private:
    double salary;
public:

    Salary();
    Salary(int, char*, int, double); //id, name, ssn, salary
    ~Salary();
    void DisplayEmployeeData();
    //getters
    double GetSalary();
    //setters
    void SetSalary(double);
};//end class Exempt

class Hourly : public Employee
{
private:
    double rate;
    double hoursWorked;
public:
    Hourly();
    Hourly(int, char*, int, double, double); //id, name, ssn, rate
    ~Hourly();
    void DisplayEmployeeData();
    //getters
    double GetRate();
    double GetHoursWorked();
    //setters
    void SetRate(double);
    void SetHoursWorked(double);
};//end Hourly Class

    const int HOURLYTYPE = 0;
    const int SALARYTYPE = 1;

//*******body*******

#include "lab05.h";


Employee::Employee(){};
Employee::Employee(int ID, char* nme, int ssn) : employeeID(ID), SSN(ssn)
{
    strcpy(name, nme);
}
int Employee::GetID()
{
    return employeeID;
}
char* Employee::getName()
{
    return name;
}
int Employee::GetSSN()
{
    return SSN;
}
void Employee::SetID(int i)
{
    employeeID = i;
}
void Employee::SetName(char* n)
{
    strcpy(name, n);
}
void Employee::SetSSN(int i)
{
    SSN = i;
}
void Employee::DisplayBaseData()
{
    cout << "ID: \t" << employeeID << endl;
    cout << "Name: \t " << name << endl;
    cout << "SSN: \t" << SSN << endl;
}
Employee::~Employee(){}


Salary::Salary(){}
Salary::Salary(int id, char* nme, int ssn, double slry) : Employee(id, nme, ssn), salary(slry){}
void Salary::DisplayEmployeeData()
{
    DisplayBaseData();
    cout << "Salary: \t " << salary << endl;
}
double Salary::GetSalary()
{
    return salary;
}

void Salary::SetSalary(double d)
{
    salary = d;
}
Salary::~Salary(){}


Hourly::Hourly(){}
Hourly::Hourly(int id, char* nme, int ssn, double rte, double worked) : Employee(id, nme, ssn), rate(rte), hoursWorked(worked){}
void Hourly::DisplayEmployeeData()
{
    DisplayBaseData();
    cout << "Rate: \t" << rate << endl;
    cout << "Worked: \t " << hoursWorked << endl;
}
double Hourly::GetRate()
{
    return rate;
}
double Hourly::GetHoursWorked()
{
    return hoursWorked;
}
void Hourly::SetRate(double d)
{
    rate = d;
}
void Hourly::SetHoursWorked(double d)
{
    hoursWorked = d;
}
Hourly::~Hourly(){}




vector<Employee*> LoadEmployeeData()
{

    vector<Employee*> employeeList;
    string fileName = "";
    cout << "\nEnter filename for employee data: ";
    cin >> fileName;
    fstream file;


    file.open(fileName, ios::in, ios::binary);
    char buffer[4096] = {0};
    int numEntries;
    file.read((char*)&numEntries, sizeof(int));
    cout << numEntries << " number of entries found." << endl;
    if (numEntries != 0)
    {
        int identifier;
        for (int i = 0; i < numEntries; i++)
        {
            file.read((char*)&identifier, sizeof(int));
            if (identifier == SALARYTYPE)
            {
                Employee* temp = new Salary();
                file.read((char*)temp, sizeof(Salary));
                employeeList.push_back(temp);
            }
            else if (identifier == HOURLYTYPE)
            {
                Employee* temp = new Hourly();
                file.read((char*)temp, sizeof(Hourly));
                employeeList.push_back(temp);
            }
        }
    }
    else cout << "No Entries found." << endl;

    file.close();
    return employeeList;
}//end LoadEmployeeData function

void ListEmployees(vector<Employee*> &e)
{
    if (e.size() != 0)
    {
        for (int i = 0; i < e.size(); i++)
        {
            if (typeid(*(e[i])) == typeid(Hourly))
            {
                cout << "\n(" << i << ")" << endl;
                dynamic_cast<Hourly*>(e[i])->DisplayEmployeeData();
            }

            else if (typeid(*(e[i])) == typeid(Salary))
            {
                cout << "\n(" << i << ")" << endl;
                dynamic_cast<Salary*>(e[i])->DisplayEmployeeData();
            }
        }
    }
    else cout << "No items in list" << endl;    
}// end ListEmployees function

void ModifyEmployee(vector<Employee*> &e)
{
    cout << "Enter employee selection." << endl;
}

void CreateEmployee(vector<Employee*> &e)
{
    bool continueLoop = true;
    srand(time(0)); //seed random number generator

    cout << "\n Enter new employee information." << endl;
    cout << "Name: ";
    char newName[80] = {0};
    cin >> newName;
    cout << "\n SSN: ";
    int newSSN;
    cin >> newSSN;
    char newType = '-1';
    do
    {
        cout << "\n Is new employee paid a (s)alary or (h)ourly rate? ";
        cin >> newType;
        if (newType == 's' || newType == 'h') continueLoop = false;
        else cout << "incorrect input" << endl;
    }while (continueLoop == true);
    if (newType == 's')
    {
        cout << "Enter salary amount: ";
        double amount;
        cin >> amount;
        e.push_back(new Salary(rand() % 1000 + 1, newName, newSSN, amount));
    }
    else if (newType == 'h')
    {
        cout << "Enter hourly amount: ";
        double amount;
        cin >> amount;
        cout << "Enter hours worked: ";
        double hoursWorked;
        cin >> hoursWorked;
        e.push_back(new Hourly(rand() % 1000 + 1, newName, newSSN, amount, hoursWorked));
    }
}

void Savelist(vector<Employee*> &e)
{
    if (e.size() == 0)
        cout << "No employees in list.  Nothing done." << endl;
    else 
    {
        cout << "Enter save filename: ";
        char fileName[80] = {'\0'};
        cin >> fileName;
        fstream* file = new fstream();
        file->open(fileName, ios::out, ios::binary);
        char buffer[80] = {'\0'};
        int numEntries = e.size();
        file->write((char*)&numEntries, sizeof(int)); //writes number of entries

        for (int i = 0; i < e.size(); i++)
        {
            if (typeid(*e[i]) == typeid(Salary))
            {   
                int classType = SALARYTYPE;
                file->write((char*)&classType, sizeof(int));
                file->write((char*)dynamic_cast<Salary*>(e[i]), sizeof(Salary));
            }
            else if (typeid(*e[i]) == typeid(Hourly))
            {
                int classType = HOURLYTYPE;
                file->write((char*)&classType, sizeof(int));
                file->write((char*)dynamic_cast<Hourly*>(e[i]), sizeof(Salary));
            }
        }
        file->close();
    }

}

void DeleteEmployee(vector<Employee*> &e)
{
    cout << "Input index number of employee to delete: ";
    int idx = 0;
    cin >> idx;
    if (idx > e.size() -1)
        cout << "invalid index number\n" << endl;
    else
    {
        delete e[idx];
        e.erase(e.begin() + idx); //removes from list
    }
}


int main()
{


    const int ZERO = 0;
    const int ONE = 1;
    const int TWO = 2;
    const int THREE = 3;
    const int FOUR = 4;
    const int FIVE = 5;
    const int SIX = 6;

    int exitMainLoop = false;  //for flow control
    int mainMenuChoice = -1;
    vector<Employee*> employeeList;
    do
    {
        cout << "Select from the following options." << endl;
        cout << "(1) Load employee data file." << endl;
        cout << "(2) View Employees." << endl;
        cout << "(3) Modify Employee data. " << endl;
        cout << "(4) Create new employee." << endl;
        cout << "(5) Save list to file." << endl;
        cout << "(6) Delete employee data. " << endl;
        cout << "(0) Exit program." << endl;

        //add more options
        cout << "Enter selection: ";
        cin >> mainMenuChoice;
        if (cin.fail())
        {
            cout << "\nInvalid selection.  Try again" << endl;
            cin.clear();
            string garbage = "";
            cin >> garbage;
        }
        else if (mainMenuChoice == ONE)
            employeeList = LoadEmployeeData();
        else if (mainMenuChoice == TWO)         
            ListEmployees(employeeList);
        else if (mainMenuChoice == THREE)
            ModifyEmployee(employeeList);
        else if (mainMenuChoice == FOUR)
            CreateEmployee(employeeList);
        else if (mainMenuChoice == FIVE)
            Savelist(employeeList);
        else if (mainMenuChoice == SIX)
            DeleteEmployee(employeeList);
        else if (mainMenuChoice == ZERO)
            exitMainLoop = true;

    }while(exitMainLoop == false);
    system("PAUSE");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:18:03+00:00Added an answer on May 18, 2026 at 4:18 am

    You can’t read/write raw C++ objects from/to disk if they have virtual methods (or use RTTI, which requires virtual methods) because there’s no guarantee that the vtable address from the first execution will be written to disk, and there’s no guarantee that the vtable will be in the same place the next time the program is run — hence, the address that was written to disk will point somewhere incorrect when it is read back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I'm having trouble with some php OO stuff. I think the code will
I am having trouble getting a rotated BufferedImage to display. I think the rotation
I'm making a program in C, and I'mm having some troubles with memory, I
I think this is a pretty simple question but I'm having trouble finding the
I think I am having trouble visualizing what code goes where and what requests
I'm having trouble retrieving the value of a cell in a gridview. I think
I'm having trouble resetting StructureMap's configuration. This only fails when I run all my
I'm having trouble with what I think should be the simplest of SQL statements,
I am having trouble embedding my applet into a webpage. I don't think I'm
I think I might be missing the point of having a logging framework for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.