forked from chihyang/CPP_Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExer08_11.cpp
More file actions
44 lines (44 loc) · 1.4 KB
/
Copy pathExer08_11.cpp
File metadata and controls
44 lines (44 loc) · 1.4 KB
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
44
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;
struct PersonInfo {
string name;
vector<string> phones;
};
int main()
{
string line, word; // will hold a line and word from input, respectively
vector<PersonInfo> people; // will hold all the records from the input
istringstream record; // move istringstream here
// read the input a line at a time until cin hits end-of-file(or another error)
while(getline(cin, line))
{
PersonInfo info; // create an object to hold this record's data
record.clear(); // WARNING: without this, we can only read one record, because after the first
// iteration, fail and eofbit will be set
record.str(line); // copy a line we just read to record
if(record.eof())
cout << "record eof" << endl;
if(record.fail())
cout << "record fail" << endl;
record >> info.name; // read the name
while(record >> word) // read the phone numbers
info.phones.push_back(word); // and store them
people.push_back(info); // append this record to people
}
for(const auto &p : people)
{
cout << p.name << " ";
for(const auto &num : p.phones)
cout << num << " ";
cout << endl;
}
return 0;
}