0

I have this main as a part of a bigger program, but every time I run the program, I only get the output of the loop once and then the program exits with a huge int as the return. How do I figure out what the problem is/ where is the problem in my code?

I've tried playing around with the limits on i to no avail.

#include <iostream>
#include <typeinfo>
#include "Employee.h"
#include "Staff.h"
#include "Education.h"
#include "Faculty.h"
#include "Parttime.h"

using namespace std;

int main() {
    Staff s1("Allen", "Paita", "123", "M", "2/23/59", 50.00);
    Staff s2("Zapata", "Steven", "456", "F", "7/12/64", 35.00);
    Staff s3("Rios", "Enrique", "789", "M", "6/2/70", 40.00);
    Education e1("PHD", "Engineering", 3);
    Education e2("PHD", "English", 1);
    Education e3("MS", "Physical Education", 0);
    Faculty f1("Johnson", "Anne", "243", "F", "4/27/62", "FU", e1);
    Faculty f2("Bouris", "William", "791", "M", "3/14/75", "AO", e2);
    Faculty f3("Andrade", "Christopher", "623", "M", "5/22/80", "AS", e3);
    Parttime p1("Guzman", "Augusto", "455", "F", "8/10/77", 35.00, 30);
    Parttime p2("Depirro", "Martin", "678", "F", "9/15/87", 30.00, 15);
    Parttime p3("Aldaco", "Marque", "678", "F", "11/24/88", 20.00, 35);
    Employee *eArray[] = {&p2, &s2, &s3, &s1, &f1, &f3, &p1, &f2, &p3};
    double totalPartTime = 0;
    double trueTotal = 0;

    for (int i = 0; i < sizeof(eArray) / sizeof(eArray[0]); i++) {
        if (typeid(*eArray[i]) == typeid(Staff)) {
            cout << "Staff: \n";
            Staff * s = dynamic_cast<Staff*>(eArray[i]);
            s->putData();
        }
        else if (typeid(*eArray[i]) == typeid(Faculty)) {
            cout << "Faculty: \n";
            Faculty * f = dynamic_cast<Faculty*>(eArray[i]);
            f->putData();
        }
        else if (typeid(*eArray[i]) == typeid(Parttime)) {
            cout << "Parttime: \n";
            Parttime * p = dynamic_cast<Parttime*>(eArray[i]);
            p->putData();
            totalPartTime += p->monthlyEarnings();
        }
        else {
            cout << "Employee: \n";
            eArray[i]->putData();
        }
        trueTotal += eArray[i]->monthlyEarnings();
        cout << endl;
    }
    cout << "Total Monthly Salary for all Part-Time Staff: " << totalPartTime << endl;
    cout << "Total Monthly Salary for all Employees: " << trueTotal << endl;


    return 0;
}

The output should be a list of outputs for each employee, but I'm only getting one output and this message:

Process exited after 0.6195 seconds with return value 3221226356

  • 5
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Nov 12 '19 at 16:36
  • Having to switch on `typeid` is always a code smell. And you need to use a debugger from that error. – underscore_d Nov 12 '19 at 16:36
  • Create a [mcve]. Use a debugger to find out where the program exits. – eerorika Nov 12 '19 at 16:36
  • 2
    Assuming `Employee` has a `virtual void putData() = 0;` method, why not just call it without all that dynamic casting? – Ted Lyngmo Nov 12 '19 at 16:39
  • 5
    FYI, that returns code is 0xC0000374 — Heap Corruption Exception – tux3 Nov 12 '19 at 16:41
  • 1
    This is a pretty flagrant abuse of OOP. `Employee` should have all the interface functionality you'll need and your code should not have to care about whether a given `Employee*` is a `Faculty`, a `Parttime` etc. - you might have virtual methods like `isParttime()` or `putData()` _in the base class_ which different derived classes can implement in different ways. `dynamic_cast` and `typeid` are strong signs that you are doing it wrong. – Max Langhof Nov 12 '19 at 16:57
  • I'll be honest, C++ is not my strong suit. I am much stronger in Java, so the transition between Java and C++ (which this class basically is) has been a bit rough. – axionevolved Nov 12 '19 at 17:06
  • 1
    It would be better not to attempt such a transition, but instead to learn C++ from the ground-up. They're two different languages with different paradigms and idioms! – Lightness Races in Orbit Nov 12 '19 at 17:07
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Nov 12 '19 at 17:14
  • 1
    If you would like to learn C++, you need to forget everything you know about Java. C++ is not Java, and C++ works in fundamentally different ways. Despite very similar syntax, C++ objects are fundamentally different, in many core ways, from Java objects, and if you try to write C++ code like you would write Java, you will only run into difficult to diagnose problems like this one. – Sam Varshavchik Nov 12 '19 at 17:18
  • Other than the odd way to handle polymorphism, I didn't see anything that stood out. I would begin trimming the for-loop code, stripping stuff out until it stops failing. That at least will give you a clue what's happening. – Joseph Larson Nov 12 '19 at 18:32

0 Answers0