I need to do a problem where i input id, hours worked, pay rate, and age. At the end i need to print id of an employee with the highest pay. I understand how to print highest pay
if (pay>highpay);
pay=highpay;
but how do i assign the id of that same employee to high pay and not the last id i have entered.
You have a couple of problems there:
1) The ; terminates the if statement and the following line will be executed unconditionally.
2) The assignment is backwards.
You want something like this:
1 2
if (pay>highpay)
highpay = pay;
To track the id of the highest paid employee, just add that to you if statement:
1 2 3 4
if (pay>highpay)
{ highpay = pay;
high_id = empid;
}
Several problems:
1) Line 43,50: When you execute these statements, id has already been replaced by the id of the next employee or -1 (line 37).
2) Line 43,50: You're using highid for two different purposes. At line 43, you're saving the id of the oldest employee. At line 50, you're saving the id of the employee that paid the most tax into the same variable. These are not necessarily the same person.
3) Lines 60-61: You're printing the id of the last employee entered at line 37, which will always be -1 for both lines. As stated in #2, these are not necessarily the same person.
Thank you very much! I finally got it. Just assigned different variable names to two IDs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if (age>oldage)
{ oldage=age;
ageid=id;
}
if (tax>mosttax)
{ mosttax=tax;
taxid=id;
}
cout << ageid << " is the oldest employee, being " << age << " years old" << endl;
cout << taxid << " employee " << "payed the most taxes, equal to $"
<< mosttax << endl;