fork download
  1. #include <iostream>
  2. #include <limits>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Login {
  9. int id;
  10. string name;
  11. string password;
  12. bool isMale;
  13. public:
  14. int getID() const { return id; }
  15. void setID(const int param) { id = param; }
  16. const string& getName() const { return name; }
  17. void setName(const string& param) { name = param; }
  18. const string& getPassword() const { return password; }
  19. void setPassword(const string& param) { password = param; }
  20. char getGender() const { return isMale ? 'M' : 'F'; }
  21. void setGender(const char param) { isMale = param == 'M' || param == 'm'; }
  22.  
  23. friend istream& operator>> (istream& lhs, Login& rhs);
  24. };
  25.  
  26. istream& operator>> (istream& lhs, Login& rhs) {
  27. char gender;
  28.  
  29. lhs >> rhs.id;
  30. lhs.ignore(numeric_limits<streamsize>::max(), ',');
  31. getline(lhs, rhs.name, ',');
  32. getline(lhs, rhs.password, ',');
  33. lhs >> ws;
  34. lhs.get(gender);
  35. rhs.isMale = gender == 'm' || gender == 'M';
  36.  
  37. return lhs;
  38. }
  39.  
  40. int main() {
  41. istringstream input("1,Liam,1234,M");
  42. Login foo;
  43.  
  44. input >> foo;
  45. cout << "ID: " << foo.getID() << " Name: " << foo.getName() << " Password: " << foo.getPassword() << " Gender: " << foo.getGender() << endl;
  46. }
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
ID: 1 Name: Liam Password: 1234 Gender: M