C++ code

/Writing Custom File Structures
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ofstream Traits("Players.txt");

cout << "ENTER PLAYER ID, NAME, MONEY\n";
cout << "press CTRL+Z to quit\n";
int idNumber;
string name;
double money;

while(cin >> idNumber >> name >> money){
Traits << idNumber << " " << name << " " << " " << money << endl;
}
}




in this code i coudnt quite understand the while loop

while(cin >> idNumber >> name >> money){
Traits << idNumber << " " << name << " " << " " << money << endl;



can someone please explain this....what does the while loop mean?
why would it stop or keep going on?
Look at the return type of the >> operator. It is a reference to istream.

For one, this allows chaining the inputs. The order of evaluation is from left to right.
1
2
3
cin >> idNumber >> name
// is same as
(cin >> idNumber) >> name


If an input fails -- the data could not be converted to type, end of file was reached, etc -- the istream will be in error state and all following inputs fail too.

Whether the >> operations succeed or not, the final evaluation of that line is essentially
while ( cin ) {
ios (base class of istream) has a conversion to bool(C++11, or void* C++98), and the above is therefore equivalent to
while ( ! cin.fail() ) {
http://www.cplusplus.com/reference/ios/ios/operator_bool/
Topic archived. No new replies allowed.