summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--8p10.cpp42
-rw-r--r--8p11.cpp43
-rw-r--r--8p12.cpp13
-rw-r--r--8p9.cpp38
-rw-r--r--section-8.3.1-program.cpp34
5 files changed, 170 insertions, 0 deletions
diff --git a/8p10.cpp b/8p10.cpp
new file mode 100644
index 0000000..6b1ce76
--- /dev/null
+++ b/8p10.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <vector>
+#include <string>
+#include <sstream>
+#include <fstream>
+
+/*
+ *
+ * 8.10
+ *
+ *
+ */
+
+int main (int argc, char **argv) {
+
+ if(argc < 2 || argc > 2) {
+ return -1;
+ }
+
+ std::ifstream file(argv[1]);
+ if(file.fail()) {
+ std::cerr << argv[0] << ": cannot open file '" << argv[1] << "'" << std::endl;
+ return -1;
+ }
+
+ std::vector<std::string> all_lines;
+ std::string cur_line;
+ while(getline(file, cur_line)) {
+ all_lines.push_back(cur_line);
+ }
+
+ for(auto &ref_all_lines : all_lines) {
+ std::istringstream is(ref_all_lines);
+ std::string is_tmp;
+ while(is >> is_tmp) {
+ std::cout << is_tmp << "\n";
+ }
+ }
+
+ std::cout << std::ends;
+ return 0;
+}
diff --git a/8p11.cpp b/8p11.cpp
new file mode 100644
index 0000000..267e692
--- /dev/null
+++ b/8p11.cpp
@@ -0,0 +1,43 @@
+#include <iostream>
+#include <vector>
+#include <sstream>
+
+/*
+ *
+ * 8.11
+ *
+ *
+ */
+
+struct PersonInfo {
+ std::string name;
+ std::vector<std::string> phones;
+};
+
+int main () {
+
+ std::string line;
+ std::string word;
+ std::vector<PersonInfo> people;
+ std::istringstream record;
+ while (getline(std::cin, line)) {
+ PersonInfo info;
+ record.clear();
+ record.str(line);
+ record >> info.name; // read the name
+ while (record >> word) {
+ info.phones.push_back(word);
+ }
+
+ people.push_back(info);
+ }
+
+ for(auto &a : people) {
+ std::cout << a.name << "\n";
+ for(auto &b : a.phones) {
+ std::cout << b << "\n";
+ }
+ }
+
+ return 0;
+}
diff --git a/8p12.cpp b/8p12.cpp
new file mode 100644
index 0000000..48dc542
--- /dev/null
+++ b/8p12.cpp
@@ -0,0 +1,13 @@
+
+/*
+ *
+ * 8.12
+ *
+ *
+ */
+
+int main () {
+
+ // Because we don't need them?
+ return 0;
+}
diff --git a/8p9.cpp b/8p9.cpp
new file mode 100644
index 0000000..5889ca2
--- /dev/null
+++ b/8p9.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <sstream>
+
+/*
+ *
+ * 8.9
+ *
+ *
+ */
+
+std::istream &read_print_report(std::istringstream &rpr) {
+
+ std::string a;
+ while(rpr >> a) {
+ std::cout << a;
+ }
+
+ if(rpr.good()) { std::cout << "good" << std::endl; }
+ if(rpr.eof()) { std::cout << "eof" << std::endl; }
+ if(rpr.fail()) { std::cout << "fail" << std::endl; }
+ if(rpr.bad()) { std::cout << "bad" << std::endl; }
+ rpr.clear();
+ std::cout << "--- cleared --- \n";
+ if(rpr.good()) { std::cout << "good" << std::endl; }
+ if(rpr.eof()) { std::cout << "eof" << std::endl; }
+ if(rpr.fail()) { std::cout << "fail" << std::endl; }
+ if(rpr.bad()) { std::cout << "bad" << std::endl; }
+ return rpr;
+}
+
+
+int main () {
+
+ std::string s = "wow i love c++!! This is so cool! I am loving these features, it really feels like C but without having to reimplement everything!";
+ std::istringstream a(s);
+ read_print_report(a);
+ return 0;
+}
diff --git a/section-8.3.1-program.cpp b/section-8.3.1-program.cpp
new file mode 100644
index 0000000..01e6de4
--- /dev/null
+++ b/section-8.3.1-program.cpp
@@ -0,0 +1,34 @@
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <vector>
+
+struct PersonInfo {
+ std::string name;
+ std::vector<std::string> phones;
+};
+
+int main () {
+
+ std::string line, word; // will hold a line and word from input, respectively
+ std::vector<PersonInfo> people; // will hold all the records from the input
+ // read the input a line at a time until cin hits end-of-file (or another error)
+ while (getline(std::cin, line)) {
+ PersonInfo info;
+ // create an object to hold this record’s data
+ std::istringstream record(line); // bind record to the line we just read
+ 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(auto &a : people) {
+ std::cout << a.name << "\n";
+ for(auto &b : a.phones) {
+ std::cout << b << "\n";
+ }
+ }
+ return 0;
+}