By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

Pass a struct array to a function

P: 87
Hey everyone. I need to pass a struct array to a function, and calculate the average of one of the members of the struct. My code is posted below.
With the current code, it takes the last value of the TotalPay variable and divides it by 25. I need to add the entire TotalPay column, and then divide by 25.

There is some info commented out, which I have used for my own troubleshooting.

Thanks,
J
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5. struct Person
  6. {
  7.     double IDNumber,HoursWorked,PayRate,TotalPay[26];
  8.     char FirstName[10],LastName[10];
  9. };
  10. void Output (Person Person_Info[])
  11. {
  12.     cout<<"LastName"<<"    "<<setw(10)<<"IDNumber"<<"  "<<setw(10)<<"TotalPay"<<endl;
  13.     for (int i=0;i<25;++i)
  14.     {
  15.         cout<<setw(6)<<Person_Info[i].LastName<<setw(14)<<Person_Info[i].IDNumber<<setw(12)<<setprecision(5)<<showpoint<<Person_Info[i].TotalPay[0]<<endl;
  16.     }
  17. }
  18. double Average (Person Person_Info[])
  19. {
  20.     double sum=0.0,avg=0.0;
  21.     int j=0;
  22.  
  23.     for (j=0;j<25;++j)
  24.     {
  25.         sum=sum+Person_Info[j].TotalPay[j];
  26.     }
  27.     avg=sum/25;
  28.     cout<<avg<<endl;
  29.     return (avg);
  30. }
  31. int main ()
  32. {
  33.     int i=0;
  34.     Person Person_Info[26]={0};
  35.     ifstream ifp;
  36.     ofstream ofp;
  37.     ifp.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\Lab 3 External Files and Structs\\Lab2.in");
  38.     ofp.open("C:\\Pay.out");
  39. //    cout<<"FirstName"<<" "<<"LastName"<<" "<<"IDNumber"<<" "<<"HoursWorked"<<" "<<"PayRate"<<" "<<"TotalPay"<<endl;
  40. //    cout<<"LastName"<<"     "<<"IDNumber"<<"     "<<"TotalPay"<<endl;
  41.     ofp<<"LastName"<<"     "<<"IDNumber"<<"     "<<"TotalPay"<<endl;
  42.     while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
  43.     {
  44.         if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)                    
  45.         {
  46.             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);                
  47.         }
  48.         else if (Person_Info[i].HoursWorked>40)                        
  49.         {
  50.             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
  51.         }
  52.     //    ofp<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  53.     //    cout<<Person_Info[i].FirstName<<"     "<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"    "<<Person_Info[i].HoursWorked<<"     "<<Person_Info[i].PayRate<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  54.     //    Average(Person_Info);
  55.     }
  56.     Average(Person_Info);
  57. //    Output (Person_Info);
  58.  
  59. }
  60.  
Jun 14 '07 #1
Share this Question
Share on Google+
8 Replies


Savage
Expert 100+
P: 1,764
Here is your problem..

Expand|Select|Wrap|Line Numbers
  1. double Average (Person Person_Info[])
  2. {
  3.     double sum=0.0,avg=0.0;
  4.     int j=0;
  5.  
  6.     for (j=0;j<25;++j)
  7.     {
  8.         sum=sum+Person_Info[j].TotalPay[j];//problem is here
  9.     }
  10.     avg=sum/25;
  11.     cout<<avg<<endl;
  12.     return (avg);
  13. }
This function will take from every Person_Info just one TotalPay.

You will need to add another loop inside.

Expand|Select|Wrap|Line Numbers
  1. for(j=0;j<25;j++)
  2. {
  3.      for(i=0;i<25;i++) sum+=Person_Info[j].TotalPay[i];//added another loop
  4. }
Savage
Jun 14 '07 #2

P: 87
That still returns the last value of the TotalPay divided by 25. So the same answer as without that loop.

Thanks,
J
Jun 14 '07 #3

Savage
Expert 100+
P: 1,764
Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2.  
  3. while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
  4.     {
  5.         if (Person_Info[i].HoursWorked<=40&&Person_Info 
  6.         [i].HoursWorked>=0)          
  7.         {
  8.             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info
  9.             [i].PayRate);    
  10.         }
  11.         else if (Person_Info[i].HoursWorked>40)      
  12.         {
  13.             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info
  14.             [i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
  15.         }
  16.        //  ofp<<Person_Info[i].LastName<<"     "<<Person_Info
  17.        [i].IDNumber<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  18.        //  cout<<Person_Info[i].FirstName<<"     "<<Person_Info
  19.        [i].LastName<<"     "<<Person_Info[i].IDNumber<<"    "<<Person_Info
  20.        [i].HoursWorked<<"     "<<Person_Info[i].PayRate<<"     "<<Person_Info
  21.        [i].TotalPay[i]<<endl;
  22.     //  Average(Person_Info);
  23.     }
I don't see that u are increasing counter i here.If u don't increase him data will be owerwriten and only last TotalPay will remain.Also whay is Totalpay array,when every time u are writing over secound person.



Savage
Jun 14 '07 #4

P: 87
Awesome. That was it, I needed to increment i. Thanks a ton, I've been staring at this program all day.


Thanks again,

J
Jun 14 '07 #5

P: 87
found the answer to this question too.


thanks again.
j
Jun 14 '07 #6

Savage
Expert 100+
P: 1,764
Awesome. That was it, I needed to increment i. Thanks a ton, I've been staring at this program all day.


Thanks again,

J
I'm more than happy to help you..

Savage
Jun 14 '07 #7

P: 87
Well I'd like to ask a quick question if it's ok. When I had the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
  3.     {
  4.         if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)                    
  5.         {
  6.             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);                
  7.         }
  8.         else if (Person_Info[i].HoursWorked>40)                        
  9.         {
  10.             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
  11.         }
  12.         ofp<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  13.         cout<<Person_Info[i].FirstName<<"     "<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"    "<<Person_Info[i].HoursWorked<<"     "<<Person_Info[i].PayRate<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  14.  

I didn't need to increment i because it was in the while loop???

This is just for my understanding.

Thanks,
J
Jun 14 '07 #8

Savage
Expert 100+
P: 1,764
No,you don't need to worry about counter incrementation only in for loop(look at for loop syntax),in do-while and while you must increase it manualy..

Savage
Jun 14 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.