### Help keeping consistent variables

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:
 ``12`` `````` if (pay>highpay) highpay = pay;``````

To track the id of the highest paid employee, just add that to you if statement:
 ``1234`` `````` if (pay>highpay) { highpay = pay; high_id = empid; }``````

could you please take a look at this, when i enter negative to stop, i get that negative number as an id.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include using namespace std; int main () { int id, highid, age, oldage; double hours, rate, pay, tax, netpay,mosttax; cout << " Please enter ID "; cin >> id; while (id >= 0) { //when negative number entered, program stops cout << " Enter hours worked "; cin >> hours; cout << " Enter the rate of pay "; cin >> rate; cout << " Enter employees' age "; cin >> age; pay = hours * rate; if (hours > 40) pay = hours * rate + ((hours - 40) * 1.5); if (age >= 55) tax = pay * 0.20; else tax= pay * 0.10; netpay = pay - tax; cout << " Employee " << id << " worked " << hours << " hours at rate of \$" << rate << " per hour, he is " << age << " years old." << endl; cout << " Tax withheld is \$" << tax << ", netpay is \$" << netpay << endl << endl; cout << "Enter the employees' ID number, negative to stop "; cin >> id; if (age>oldage) { oldage=age; highid=id; } if (tax>mosttax) { mosttax=tax; highid=id; } } cout << id << " is the oldest employee is " << age << " old" << endl; cout << id << " employee " << "payed the most taxes, equal to \$" << tax << endl; return 0; } ``````

Last edited on
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.

Last edited on
Thank you very much! I finally got it. Just assigned different variable names to two IDs.
 ``1234567891011121314151617`` `````` 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; ``````
Line 14: Should be oldage, not age. age will give you the age of the last employee, not the oldest.
Topic archived. No new replies allowed.