0

Currently working on a homework assignment, and I've read my book. I'm supposed to create a class that has an array in it, for hours worked by each person. All fine and dandy, except that when I leave a function and return to main, anything that was changed in that class is now back to what it was before, or atleast, that's how I see it. But this isn't even my problem right now, I'm trying to create an array in my gethours function, then pass it's pointer of said array back to my main function, then pass the pointer to display function. However, it's just not working. Like, at all.

CODE

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class payroll {
public:
    int employeeHours[5];
    string employeeName[5] = { "David", "Steve", "Vanessa", "Groot", "Poyo" };
    double hourlyPay = 12.55;
};

int getHours() {
    payroll payroll;
    int hours[5] = {0,0,0,0,0};
    for (int i = 0; i != 5; i++) {
        cout << "How many hours did " << payroll.employeeName[i] << " work? ";
        cin >> hours[i];
        cout << endl;
    }


    return *hours;
}

int *display(int *hours) {
    payroll payroll;
    for (int i = 0; i != 5; i++) {
        cout << payroll.employeeName[i] << " has " << *(hours + i) << " hours. " << i << endl;
    }
    return 0;
}

int main() {
    payroll payroll;
    int *hours;
    hours = new int[5];
    *hours = getHours();
    for (int i = 0; i != 5; i++) { //Testing, ignore
        cout << payroll.employeeName[i] << " has " << *(hours + i) << " hours. " << i << endl;
    }
    display(hours);
    system("Pause");
}

Now you may look at this and wonder, "Why if the assignment asks for the class to hold the array, do you have another array?" Well for me, I tried using the class array, which worked fine and dandy all inside the same function, but as said before, once I leave that function, it gets reset. So, I'm using a separate array as testing until I can figure out how I'm actually storing the variables.

John Olivas
  • 309
  • 1
  • 2
  • 14
  • 2
    Wow... there is so much wrong with that code... Why aren't you using a `vector`? – Nicol Bolas Feb 28 '16 at 07:26
  • Do not use pointers or arrays. – n. m. could be an AI Feb 28 '16 at 07:27
  • We havn't come across those in the book yet, never actually heard of it either lol, only heard of vector with geometry – John Olivas Feb 28 '16 at 07:27
  • @n.m. But.. x.x I don't understand, it tells me use an array lol – John Olivas Feb 28 '16 at 07:29
  • Who tells you that? Anyway I have closed the question as a duplicate. The code have more problemns though. – n. m. could be an AI Feb 28 '16 at 07:31
  • You can and should use `std::array` where appropriate. Don't use C-style arrays (the ones you need to declare with square brackets). If you have a problem with std::array, ask a question about std::array. – n. m. could be an AI Feb 28 '16 at 07:33
  • Well, my teacher told us to use an array. Also, I didn't even know the difference between the things, as our book hasn't even told the difference between the bracket array and your std::array. I figured they were pretty much the same thing. But, thanks for the information, I'll look into it more and try to figure it out. – John Olivas Feb 29 '16 at 19:52

0 Answers0