I'm trying to return a struct array from two functions. I don't know what to say. Here is my code:

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

struct worker {
    int    idNumber;
    int    hoursWorked;
    double hourlyRate;
    double earned;
};

[B]???[/B]  input();
[B]???[/B]  calculateEarned(worker []);
void outputWorkers(worker []);
int  overtimeWorkers(worker []);

const double OVERTIME_RATE = 1.5;

int main() {

    outputWorkers(calculateEarned(input()));
    

    return 0;
    
}

[B]???[/B] input() {

    int a;
    cout << "Enter number of workers: ";
    cin >> a;
    cout << endl;
    [B]static worker w[a]; // is this right??[/B]
   [B] return w;[/B]

}

[B]???[/B] calculateEarned(worker w[]) {


    cout << "enter the ID numbers of the " << sizeof(w) << " workers: ";

    for(int i = 0; i < sizeof(w); i++)
        cin >> w[i].idNumber;
    
    cout << endl << "enter the hours worked for the " << sizeof(w) << " workers: ";
    
    for(int k = 0; k < sizeof(w); k++)
        cin >> w[k].hoursWorked;
    
    cout << endl << "enter the base pay for each of the " << sizeof(w) << " workers: ";
    
    for(int a = 0; a < sizeof(w); a++)
        cin >> w[a].hourlyRate;
    
    return w;
}

void outputWorkers(worker w[]) {

    cout << setw(10) << "worker" << setw(10) << "rate" << setw(10) << "hours" << setw(10) << "pay"
         << endl << endl;
    
    for(int i = 0; i < sizeof(w); i++) {
    
    if(w[i].hoursWorked > 40)
        w[i].earned = static_cast<double>(w[i].hoursWorked - 40) * w[i].hourlyRate * OVERTIME_RATE
                      + static_cast<double>(40) * w[i].hourlyRate;
    else
        w[i].earned = static_cast<double>(w[i].hoursWorked) * w[i].hourlyRate;
    
        cout << fixed << setprecision(2)
             << setw(10) << w[i].idNumber
             << setw(10) << w[i].hourlyRate
             << setw(10) << w[i].hoursWorked
             << setw(10) << w[i].earned
             << endl;
    }
}

int overtimeWorkers(worker w[]) {

    int count;    
    for(int i = 0; i < sizeof(w); i++)
        if(w[i].hoursWorked > 40)
            count = i + 1;
    
    return count;
}

there are two ways to declare a one-dimentional array

(1)??? calculateEarned(worker array[]);

and
(2)??? calculateEarned(worker * array);

but there is only one way to declare the return value, using a pointer to the beginning of the array.
worker* calculateEarned(worker []);

ok, thanks, i will work on it

to simplify:

worker* input() {

    int a;
    cout << "Enter number of workers: ";
    cin >> a;
    cout << endl;
    worker w[a]; // is this right??
    return w;

}

and then, say: nextCall(input());

how does this work?

I'm getting "returning a local variable" errors, and "size of w[] is not constant," and other, more inscrutable errors.

here are the errors of the following code..

workerlist.cpp: In function ‘int main()’:
workerlist.cpp:24: error: ‘w’ was not declared in this scope
jess@linux:~/cpp/labs10+11/Lab#10$ g++ workerlist.cpp
workerlist.cpp: In function ‘int main()’:
workerlist.cpp:24: error: ‘w’ was not declared in this scope
workerlist.cpp: In function ‘void input()’:
workerlist.cpp:39: error: storage size of ‘w’ isn't constant

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

struct worker {
    int    idNumber;
    int    hoursWorked;
    double hourlyRate;
    double earned;
};

void input();
void  calculateEarned(worker []);
void outputWorkers(worker []);
int  overtimeWorkers(worker []);

const double OVERTIME_RATE = 1.5;

int main() {

    input();
    calculateEarned(w);
    outputWorkers(w);
    cout << overtimeWorkers(w) << " workers with overtime hours" << endl;
    

    return 0;
    
}

void input() {

    int a;
    cout << "Enter number of workers: ";
    cin >> a;
    cout << endl;
    static worker w[a]; // is this right??
    
}

void calculateEarned(worker w[]) {


    cout << "enter the ID numbers of the " << sizeof(w) << " workers: ";

    for(int i = 0; i < sizeof(w); i++)
        cin >> w[i].idNumber;
    
    cout << endl << "enter the hours worked for the " << sizeof(w) << " workers: ";
    
    for(int k = 0; k < sizeof(w); k++)
        cin >> w[k].hoursWorked;
    
    cout << endl << "enter the base pay for each of the " << sizeof(w) << " workers: ";
    
    for(int a = 0; a < sizeof(w); a++)
        cin >> w[a].hourlyRate;
    
}

void outputWorkers(worker w[]) {

    cout << setw(10) << "worker" << setw(10) << "rate" << setw(10) << "hours" << setw(10) << "pay"
         << endl << endl;
    
    for(int i = 0; i < sizeof(w); i++) {
    
    if(w[i].hoursWorked > 40)
        w[i].earned = static_cast<double>(w[i].hoursWorked - 40) * w[i].hourlyRate * OVERTIME_RATE
                      + static_cast<double>(40) * w[i].hourlyRate;
    else
        w[i].earned = static_cast<double>(w[i].hoursWorked) * w[i].hourlyRate;
    
        cout << fixed << setprecision(2)
             << setw(10) << w[i].idNumber
             << setw(10) << w[i].hourlyRate
             << setw(10) << w[i].hoursWorked
             << setw(10) << w[i].earned
             << endl;
    }
}

int overtimeWorkers(worker w[]) {

    int count;    
    for(int i = 0; i < sizeof(w); i++)
        if(w[i].hoursWorked > 40)
            count = i + 1;
    
    return count;
}

Error Returning a local variable

You are gettin this error since you are trying to return worker type array "w" which is local to the function and hence is destroyed when the function exits. Said simply the scope or the effect of the local variable is in the function itself since its memory is allocated in the stack of local variables which is destroyed when the function exits.

Errror size of w[] not constant

In C or C++ u cant declare an array like this i.e. an array whose size the compiler has to calculate at run time. C expects that you explicitly declare the size of the array during compile time.

Hence the acceptable options are

worker w [10];

OR

const int numberOfItems = 10;
worker w [numberOfItems];

Bye.

alright.. i had to rearrange all the functions to accommodate that fact, but it's done now. forget about returning pointers, for now..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.