Fuction to read in and sum data to a record:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dizzylizzyd514
    New Member
    • Jul 2008
    • 5

    Fuction to read in and sum data to a record:

    void get_workdata(if stream& in, emp_rec anc_emp[], int n)
    {
    int x;
    double y;
    int cnt = 0;
    for (int i=0; i<n; i++)
    {
    anc_emp[i].hoursworked = 0.0;
    anc_emp[i].commission = 0.0;
    }
    in >> x;
    while (x >= 0)
    {
    in >> y;
    for (int i=0; i<n; i++)
    {
    if (x = anc_emp[i].idnum)
    {
    anc_emp[i].hoursworked = anc_emp[i].hoursworked + y;
    cnt = cnt;
    }
    if (x != anc_emp[i].idnum)
    {cnt++;
    }
    }
    if (cnt = n)
    {
    cout << "Error: unassignmed ID# " << x << " with " << y << " hours."
    << endl;
    }
    in >> x;
    }

    }








    So basically the first data set is this:
    (employee name, idnumber, payrate)
    tom 46 7.50
    saLLy 53 8.25
    VanEssA 91 5.85

    The second data set is this:
    (idnumber, hours worked)
    91 10.0
    11 15.0
    46 8.0
    91 4.5
    53 20.0
    91 6.75
    56 40.0
    -3

    I am supposed to create employee records and sum the hours worked and figure the total pay. So far, I have everything down EXCEPT for my function that reads in the idnumber, then the hours worked for that idnumber. If the id matches an id from file1, the hours need to be added. If the id number doesn't match, an error message will print that states it's an invailid id number. I denoted 'x' as the idnumber being read in and 'y' as the hours worked for that id number.

    Here is my algorithm:

    1. Read in the first id number
    2. While id number is not negative,
    3. Read in hours worked.
    4. Set up a For loop
    a) If x equals an idnumber from file 1, then add the hours worked to that
    record.
    b) Else: add one to 'cnt'

    After for loop runs through all the id numbers from file 1,
    5. If cnt = n (the number of employee records) then the current x value doesn't match any of the id numbers from file1 and is an invalid id number and a message will print.

    6. Read in x (next id number)

    My program compiles, but when I run it, I get this:

    Error: unassigned ID# 91 with 10 hours.
    Error: unassigned ID# 91 with 15 hours.
    Error: unassigned ID# 91 with 8 hours.
    Error: unassigned ID# 91 with 4.5 hours.
    Error: unassigned ID# 91 with 20 hours.
    Error: unassigned ID# 91 with 6.75 hours.
    Error: unassigned ID# 91 with 40 hours.

    Could anyone please look at my function get_workdata and see if you can find any errors because I am stuck!
    Last edited by dizzylizzyd514; Jul 12 '08, 12:08 AM. Reason: Posted whole program code by mistake.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You need a second = in the if condition Anywhere = is legal syntax, == is as well, which can lead to some interesting errors...

    Also, please try to use CODE tags when you post code.

    Comment

    • dizzylizzyd514
      New Member
      • Jul 2008
      • 5

      #3
      With the '==' after my if statement, this is what I'm getting:

      Error: unassignmed ID# 91 with 10 hours.
      Error: unassignmed ID# 11 with 15 hours.
      Error: unassignmed ID# 46 with 8 hours.
      Error: unassignmed ID# 91 with 4.5 hours.
      Error: unassignmed ID# 53 with 20 hours.
      Error: unassignmed ID# 91 with 6.75 hours.
      Error: unassignmed ID# 56 with 40 hours.

      I only want 11, 56, and 67 to display error messages. I can't figure out why all the id numbers are displaying error messages. Any other ideas?

      Comment

      • dizzylizzyd514
        New Member
        • Jul 2008
        • 5

        #4
        Nevermind I worked it out.

        Comment

        Working...