reading from data file

Feb 6, 2019 at 1:19am
I am having some issues with my case for the code. The purpose of this case is that, the user enters a number of hours and the if the hours worked by the employee is less than the hours entered by the user, it will be read from a data file and displayed. Now, if the data file has no hours worked less than hours specified by the user, it will show cout << "NO RECORDS FOUND!\n";. When i do a test run, it shows no record found, more than 1 time. How can i get out of the counter. I tried to do this with if else logic.

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
case 3: {
  ifstream fin("personnel.dat");
  cout << "Please enter the hours:\n";
  cin >> hours;

  int counter = 0;
  {
    while (fin >> employeeID >> employeeLASTNAME >> employeeFIRSTNAME >>
           hoursWORKED >> hourlyPAYRATE >> federalTAXRATE >> stateTAXRATE)

      if (hoursWORKED < hours)

          {
        cout << "\nRecord number: " << counter << endl
             << "Employee ID: " << employeeID << endl
             << "Last name: " << employeeLASTNAME << endl
             << "First name: " << employeeFIRSTNAME << endl
             << "Hours worked: " << hoursWORKED << setprecision(2) << endl
             << "Pay per hour: $" << hourlyPAYRATE << setprecision(2) << endl
             << "Federal tax rate: " << federalTAXRATE << "%" << setprecision(2)
             << endl  // this and next percent
             << "State Tax rate: " << stateTAXRATE << "%" << setprecision(2)
             << endl
             << "Gross Pay: $" << grosspay(hoursWORKED, hourlyPAYRATE)
             << setprecision(2) << endl
             << "Federal Taxes: $" << federaltaxes(grossPAY, federalTAXRATE,
                                                   hoursWORKED, hourlyPAYRATE)
             << setprecision(2) << endl
             << "State Taxes: $"
             << statetaxes(hoursWORKED, hourlyPAYRATE, stateTAXRATE)
             << setprecision(2) << endl
             << "Net Pay: $" << netpay(hoursWORKED, hourlyPAYRATE, grossPAY,
                                       federalTAXRATE, stateTAXRATE)
             << setprecision(2) << endl;
        counter++;
      }

      else {
        cout << "NO RECORDS FOUND!\n";
      }
  }
  fin.close();
  counter = 0;
  system("pause");
}
Feb 6, 2019 at 2:17am
not entirely sure what you are asking, but … hoursworked < hours will fail if they are equal. From what I see, I would expect them to BE equal, but I dunno exactly what you want it to DO.

also, the IF is part of the while, and so is the else, so yes it will do it multiple times. Did you mean to put a ; on that while loop and do the if one time?
Last edited on Feb 6, 2019 at 2:18am
Feb 6, 2019 at 2:24am
I am trying to look through a data file and display all the records for people whose hours worked is less than the hours user is asked to enter.
The only problem i am having is that.
if i enter 2, when asked to put in hours. NO RECORDS FOUND is written to terminal many times. What can i do, so it is only written one time.
Feb 6, 2019 at 5:12am
Well you remove the offending print statement from inside your while loop.

Then you do
1
2
3
if ( count == 0 ) {
    cout << "NO RECORDS FOUND!\n";
}
Feb 6, 2019 at 5:25am
Thanks @salem c!
Topic archived. No new replies allowed.