Data file read

Hello everyone,
I am trying to read from a data file. I have a working code, but i am having difficulty on how i can display rest of the data in the data file. Right now, it is only reading 2 things, but there are 26 lines total. I have done the while loop in the code. Next line to read would be...
AS2333 Tralfaz Henri 34.50 23.45 0.025 0.03
and so forth
here is the 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
case 2:

            {
                ifstream fin("personnel.dat");
             // if (fin.is_open())


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

            {
                counter++;

             cout<<"\nRecord number: "<<counter<< endl
                    << "Employee ID: " <<employeeID<<endl
                    << "Last name: " <<employeeLASTNAME<<endl
                    << "First name: " <<employeeFIRSTNAME<< endl
                    << "Hours worked: " << hoursWORKED<<endl
                    << "Pay per hour: $" << hourlyPAYRATE<<endl
                    << "Federal tax rate:"<< federalTAXRATE<<endl
                  << "State Tax rate: " <<stateTAXRATE<<endl;

           fin >> employeeID >> employeeLASTNAME >>employeeFIRSTNAME
                >> hoursWORKED >>hourlyPAYRATE >> federalTAXRATE
                >> stateTAXRATE;
            }
            fin.close();
            counter =0;

        }

        }
   }
Last edited on
It's confusing because your indentation is a mess.
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
  {
    ifstream fin("personnel.dat");
    // if (fin.is_open())
    int counter = 0;
    while (!fin.eof()) {
      fin >> employeeID >> employeeLASTNAME >> employeeFIRSTNAME
          >> hoursWORKED >> hourlyPAYRATE >> federalTAXRATE >> stateTAXRATE;
      {
        counter++;
        cout << "\nRecord number: " << counter << endl
            << "Employee ID: " << employeeID << endl
            << "Last name: " << employeeLASTNAME << endl
            << "First name: " << employeeFIRSTNAME << endl
            << "Hours worked: " << hoursWORKED << endl
            << "Pay per hour: $" << hourlyPAYRATE << endl
            << "Federal tax rate:" << federalTAXRATE << endl
            << "State Tax rate: " << stateTAXRATE << endl;

        fin >> employeeID >> employeeLASTNAME >> employeeFIRSTNAME
            >> hoursWORKED >> hourlyPAYRATE >> federalTAXRATE >> stateTAXRATE;
      }
      fin.close();
      counter = 0;
    }
  }


fin.close(); is INSIDE your while loop.
So yes, things finish before you expect.
This is fundamentally wrong:
1
2
3
4
5
6
7
8
9
10
while ( !fin.eof() ) {

    // try to read things from fin
    fin >> employeeID >> employeeLASTNAME >>employeeFIRSTNAME
        >> hoursWORKED >>hourlyPAYRATE >> federalTAXRATE >> stateTAXRATE ;
    // this attempted input might have failed
    // but assume that it would never fail and print out what might have been read
    ++counter ;
    std::cout << ....
}


This is the canonical way to write the loop:
1
2
3
4
5
6
7
8
9
10
// repeat till there is nothing more to read in (till input fails)
// execute the body of the loop only if the attempted input operation is successful
while ( fin >> employeeID >> employeeLASTNAME >>employeeFIRSTNAME
            >> hoursWORKED >>hourlyPAYRATE >> federalTAXRATE >> stateTAXRATE ) {
               
    // we know that the attempted input was successful
    // print out what was read from the file  
    ++counter ;
    std::cout << ....
}


This operator makes it possible to use streams and functions that return references to streams as loop conditions, resulting in the idiomatic C++ input loops such as while(stream >> value) {...} or while(getline(stream, string)){...}. Such loops execute the loop's body only if the input operation succeeded.
https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
Thank you everyone for being great help! It works now and also fixed the fundamentals!
Topic archived. No new replies allowed.