Making a read function that allows spaces in the input (C++) -
"write class named person represents name , address of person. use string hold each of these elements. add operations read , print person objects code wrote."
note haven't reached section on access control yet.
#include <iostream> #include <string> using std::string; struct person { string name_var, address_var; }; std::ostream &print(std::ostream&, const person &); std::istream &read(std::istream&, person &); std::istream &read(std::istream &is, person &item) { >> item.name_var >> item.address_var; return is; } std::ostream &print(std::ostream &os, person &item) { os << item.name_var << " " << item.address_var; return os; } with can read single worded names , addresses if use std::cin first argument read, isn't useful. can somehow use getline?
you can use:
std::getline(is, item.name_var); you can specify delimiter char third argument
Comments
Post a Comment