-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex8_13.cpp
56 lines (49 loc) · 1.21 KB
/
ex8_13.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Exercise 8.13: Rewrite the phone number program from this section to read
* from a named file rather than from cin.
*/
#include "iostream"
#include "vector"
#include "sstream"
#include "fstream"
void validate(const std::string &);
int main(int argc, char **argv) {
if (argc == 1) {
std::cout << "usage: " << *argv << " ADDRESS_BOOK_PATH" << std::endl;
return -1;
}
std::ifstream ifs(*++argv);
if (!ifs) {
std::cout << *argv << ": read failed." << std::endl;
return -1;
}
std::string bufl;
while (std::getline(ifs, bufl))
validate(bufl);
}
struct Person {
std::string name;
std::vector<std::string> phones;
};
inline bool valid(const std::string &);
void validate(const std::string &line_) {
std::istringstream line(line_);
Person person;
line >> person.name; /// get name
std::string phone;
while (line >> phone) {
if (!valid(phone)) {
std::cerr << "Warning: Wrong phone number: " << person.name <<
": " << phone << std::endl;
return;
}
person.phones.emplace_back(phone);
}
std::cout << person.name << ": ";
for (const auto &num : person.phones)
std::cout << num << " ";
std::cout << std::endl;
}
inline bool valid(const std::string &phone) {
return phone.size() == 3;
}