| | |
need to pass and return arrays, how?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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;
} to simplify:
how does this work?
I'm getting "returning a local variable" errors, and "size of w[] is not constant," and other, more inscrutable errors.
C++ Syntax (Toggle Plain Text)
worker* input() { int a; cout << "Enter number of workers: "; cin >> a; cout << endl; worker w[a]; // is this right?? return w; } and then, say: <span class="ad_notxt"><code class="inlinecode">nextCall(input());</code></span>
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
C++ Syntax (Toggle Plain Text)
#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.
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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Similar Threads
- need help understandin how to pass by reference. (Java)
- arrays??? (C++)
- Need help with recursion and arrays (C++)
- Return multidimension array (C)
Other Threads in the C++ Forum
- Previous Thread: question about returnin structs, w/ a bug..
- Next Thread: complete binary tree using an array help
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






