loop problem

How to display name of the highest hours worked as an output after the loop
How can we help you without knowing more about your code?
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
#include<iostream.h>
#include<conio.h>

int main()
{
  //declaration
  int count1=0, count2=0, count3=0;
  float hours, avg, sum=0.0, min=0.0, max=168;
  char name[51];

  //input
  cout<<"Please enter first employee's name :"<<endl;
  cin.getline(name,50);
  cout<<"Please enter number of hours employee worked per week : ";
  cin>>hours;

  while(hours != -1)
  //-1 is a sentinel value
  {
     //if...else statement(selection)
     if(hours>55)
       { count1++;
         if(max<=hours)
            max=hours; }

     else if(hours>=35 && hours<=55)
             count2++;

          else if(hours<35)
                 { count3++;
         		    if(min>=hours)
            	    min=hours; }

     sum+=hours;
     cout<<endl;

     cin.ignore(80,'\n');
     cout<<"Please enter next employee's name :"<<endl;
     cin.getline(name,50);
     cout<<"Please enter number of hours employee worked per week and enter -1 to stop processing : ";
     cin>>hours;
   }

   //process
   avg=sum/(count1+count2+count3);

   //output
   cout<<"The number of employees in each category :"<<endl;
   cout<<"HIGHLY PRODUCTIVE : "<<count1<<endl;
   cout<<"SATISFACTORY : "<<count2<<endl;
   cout<<"OVERPAID : "<<count3<<endl;
   cout<<"The highest number of hours employee worked is "<<max<<endl;
   cout<<"The lowest number of hours employee worked is "<<min<<endl;
   cout<<"The average hours worked : "<<avg;

getch();
return 0;
}




Hint: When you set the max variable in the if block on lines 23-24, you could do more than just that. For example, also storing the name of the employee to another max variable. (You will have to name that variable something other than max of course, since that name is already taken.)
Topic archived. No new replies allowed.