1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <string>
#include <sstream>
#include <iostream>
class Person {
std::string ID;
std::string name;
std::string address;
std::string phone;
public:
Person(std::string ID0, std::string name0, std::string address0, std::string phone0)
: ID(ID0), name(name0), address(address0), phone(phone0) {} // proper syntax
Person() {}
void show() const {
std::cout << "Person created with ID " << ID << " and phone " << phone << '\n';
}
friend std::istream& operator>>(std::istream& is, Person& p)
{
getline(is, p.ID);
getline(is, p.name);
getline(is, p.address);
getline(is, p.phone);
return is;
};
};
int main()
{
std::istringstream test("529173860\n"
"Dick B. Smith\n"
"879 Maple Road, Centralia, Colorado 24222\n"
"(312) 000-1000\n"
"925173870\n"
"Harry C. Anderson\n"
"635 Main Drive, Midville, California 48444\n"
"(660) 050-2200");
Person p;
while(test >> p) // proper input loop
{
p.show();
}
}
|