Array of records

Hello, given an input file with the following information, I would like to write a program that will accumulate the total hours worked for each employee using array of records(structs).

I have written the following function so far but it I have been geting -nan as my result..... I believe the problem is that I need to initialize "name[i].total_hr =0.0" so I can accumulate the total hours.

Note: I only pasted a part of my code since the whole program is pretty big, please let me know if any additional information about my code is needed.

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
struct emp_data
{
  string employee_name;
  int ID_num;
  double hr_pay;
  double commission;
  double work_hr;
  double total_hr;
  double total_com;
};

int find(emp_data name[], int n, int ID_nums)
{
  for (int i=0; i<n; i++)
    if (name[i].ID_num == ID_nums)
      return i;
  return -1;
}

 

void read_work_data(emp_data name[],int n)
{
  ifstream infile;
  string filename;
  int ID_nums,verify;
  double hrs_work;
  cout << "Enter file name" << endl;
  cin >> filename;
  infile.open(filename.c_str());

  infile >> ID_nums;
  while (ID_nums > 0)
    {                                                                                                                                                                                                                                       
      infile >> hrs_work;
      verify = find (name,n,ID_nums);
      if (verify >=0)
        {
          for (int i=0; i<n; i++)
              if (name[i].ID_num == ID_nums)                                                                                                                                                                                                                                                   
                  name[i].total_hr = hrs_work + name[i].total_hr;                                          
        }
}      

      infile >> ID_nums ;

    }




The following is what the input file is going to look like(ID number Hours work):

49 5.0
46 8.0
49 8.0
24 10.0
49 10.0
24 5.0


Expected output:

ID# HOURS
49 23.00
46 8.00
24 15.00
Topic archived. No new replies allowed.