431,661 Members | 779 Online
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 #include  #include  #include  using namespace std; struct Person {     double IDNumber,HoursWorked,PayRate,TotalPay[26];     char FirstName[10],LastName[10]; }; void Output (Person Person_Info[]) {     cout<<"LastName"<<"    "<>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)     {         if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)                             {             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);                         }         else if (Person_Info[i].HoursWorked>40)                                 {             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);         }     //    ofp<
8 Replies

 Expert 100+ P: 1,764 Here is your problem.. Expand|Select|Wrap|Line Numbers double Average (Person Person_Info[]) {     double sum=0.0,avg=0.0;     int j=0;       for (j=0;j<25;++j)     {         sum=sum+Person_Info[j].TotalPay[j];//problem is here     }     avg=sum/25;     cout<

 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

 Expert 100+ P: 1,764 Expand|Select|Wrap|Line Numbers int i=0;   while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)     {         if (Person_Info[i].HoursWorked<=40&&Person_Info          [i].HoursWorked>=0)                   {             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info             [i].PayRate);             }         else if (Person_Info[i].HoursWorked>40)               {             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info             [i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);         }        //  ofp<

 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

 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   while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)     {         if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)                             {             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);                         }         else if (Person_Info[i].HoursWorked>40)                                 {             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);         }         ofp<

 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