#include #include #include using namespace std; class Person { public: Person(): name{}, age{} {} friend istream& operator>>(istream & lhs, Person & rhs) { cout << "Setting the Person objects datamembers using istream" << endl; string n; char trash; int a; lhs >> n >> trash >> a; rhs.name = n; if( a > 0 ) { rhs.age = a; } else //input is wrong { lhs.setstate(ios_base::failbit); cerr << "Incorrect input, age needs to be >= 0" << endl; rhs.age = 0; } return lhs; } private: string name; int age; }; int main() { // Utan problem Person p1{}; istringstream iss1{"Pontus , 30"}; iss1 >> p1; cout << "Failbit is set: "; if( iss1.fail() ) { cout << "True" << endl; } else { cout << "False" << endl; } cout << '\n'; // Med problem (fel format på inmatning) Person p2{}; istringstream iss2{"Janos , -25"}; iss2 >> p2; cout << "Failbit is set: "; if( iss2.fail() ) { cout << "True" << endl; } else { cout << "False" << endl; } }