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:
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;
  }



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

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
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.
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; 
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.