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
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