944,088 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3131
  • C++ RSS
Jul 18th, 2006
0

need to pass and return arrays, how?

Expand Post »
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;
};

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

const double OVERTIME_RATE = 1.5;

int main() {

    outputWorkers(calculateEarned(input()));
    

    return 0;
    
}

??? input() {

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

}

??? 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;
}
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Jul 18th, 2006
0

Re: need to pass and return arrays, how?

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 []);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Jul 18th, 2006
0

Re: need to pass and return arrays, how?

ok, thanks, i will work on it
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Jul 18th, 2006
0

Re: need to pass and return arrays, how?

to simplify:
C++ Syntax (Toggle Plain Text)
  1. worker* input() {
  2.  
  3. int a;
  4. cout << "Enter number of workers: ";
  5. cin >> a;
  6. cout << endl;
  7. worker w[a]; // is this right??
  8. return w;
  9.  
  10. }
  11.  
  12. and then, say: <code class="inlinecode">nextCall(input());</code>
how does this work?

I'm getting "returning a local variable" errors, and "size of w[] is not constant," and other, more inscrutable errors.
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Jul 18th, 2006
0

Re: need to pass and return arrays, how?

here are the errors of the following code..
Quote ...
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
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. struct worker {
  6. int idNumber;
  7. int hoursWorked;
  8. double hourlyRate;
  9. double earned;
  10. };
  11.  
  12. void input();
  13. void calculateEarned(worker []);
  14. void outputWorkers(worker []);
  15. int overtimeWorkers(worker []);
  16.  
  17. const double OVERTIME_RATE = 1.5;
  18.  
  19. int main() {
  20.  
  21. input();
  22. calculateEarned(w);
  23. outputWorkers(w);
  24. cout << overtimeWorkers(w) << " workers with overtime hours" << endl;
  25.  
  26.  
  27. return 0;
  28.  
  29. }
  30.  
  31. void input() {
  32.  
  33. int a;
  34. cout << "Enter number of workers: ";
  35. cin >> a;
  36. cout << endl;
  37. static worker w[a]; // is this right??
  38.  
  39. }
  40.  
  41. void calculateEarned(worker w[]) {
  42.  
  43.  
  44. cout << "enter the ID numbers of the " << sizeof(w) << " workers: ";
  45.  
  46. for(int i = 0; i < sizeof(w); i++)
  47. cin >> w[i].idNumber;
  48.  
  49. cout << endl << "enter the hours worked for the " << sizeof(w) << " workers: ";
  50.  
  51. for(int k = 0; k < sizeof(w); k++)
  52. cin >> w[k].hoursWorked;
  53.  
  54. cout << endl << "enter the base pay for each of the " << sizeof(w) << " workers: ";
  55.  
  56. for(int a = 0; a < sizeof(w); a++)
  57. cin >> w[a].hourlyRate;
  58.  
  59. }
  60.  
  61. void outputWorkers(worker w[]) {
  62.  
  63. cout << setw(10) << "worker" << setw(10) << "rate" << setw(10) << "hours" << setw(10) << "pay"
  64. << endl << endl;
  65.  
  66. for(int i = 0; i < sizeof(w); i++) {
  67.  
  68. if(w[i].hoursWorked > 40)
  69. w[i].earned = static_cast<double>(w[i].hoursWorked - 40) * w[i].hourlyRate * OVERTIME_RATE
  70. + static_cast<double>(40) * w[i].hourlyRate;
  71. else
  72. w[i].earned = static_cast<double>(w[i].hoursWorked) * w[i].hourlyRate;
  73.  
  74. cout << fixed << setprecision(2)
  75. << setw(10) << w[i].idNumber
  76. << setw(10) << w[i].hourlyRate
  77. << setw(10) << w[i].hoursWorked
  78. << setw(10) << w[i].earned
  79. << endl;
  80. }
  81. }
  82.  
  83. int overtimeWorkers(worker w[]) {
  84.  
  85. int count;
  86. for(int i = 0; i < sizeof(w); i++)
  87. if(w[i].hoursWorked > 40)
  88. count = i + 1;
  89.  
  90. return count;
  91. }
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Jul 18th, 2006
0

Re: need to pass and return arrays, how?

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.
Last edited by ~s.o.s~; Jul 18th, 2006 at 4:38 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Jul 18th, 2006
0

Re: need to pass and return arrays, how?

alright.. i had to rearrange all the functions to accommodate that fact, but it's done now. forget about returning pointers, for now..
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: question about returnin structs, w/ a bug..
Next Thread in C++ Forum Timeline: complete binary tree using an array help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC