From d5fcdab87a013b2c919b66ea9a52b469620ca49f Mon Sep 17 00:00:00 2001 From: Oskar Date: Sat, 5 Oct 2024 14:23:02 +0200 Subject: more --- 8p1.cpp | 34 ++++++++++++++++++++++++++++++++++ 8p2.cpp | 34 ++++++++++++++++++++++++++++++++++ 8p3.cpp | 13 +++++++++++++ tie-test.cpp | 24 ++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 8p1.cpp create mode 100644 8p2.cpp create mode 100644 8p3.cpp create mode 100644 tie-test.cpp diff --git a/8p1.cpp b/8p1.cpp new file mode 100644 index 0000000..1ad9fe5 --- /dev/null +++ b/8p1.cpp @@ -0,0 +1,34 @@ +#include + +/* + * + * 8.1 + * + * + */ + +std::istream &read_print_report(std::istream &rpr) { + + int 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 () { + + read_print_report(std::cin); + return 0; +} diff --git a/8p2.cpp b/8p2.cpp new file mode 100644 index 0000000..653e4be --- /dev/null +++ b/8p2.cpp @@ -0,0 +1,34 @@ +#include + +/* + * + * 8.2 + * + * + */ + +std::istream &read_print_report(std::istream &rpr) { + + int 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 () { + + read_print_report(std::cin); + return 0; +} diff --git a/8p3.cpp b/8p3.cpp new file mode 100644 index 0000000..8f215bb --- /dev/null +++ b/8p3.cpp @@ -0,0 +1,13 @@ + +/* + * + * + * + * + */ + +int main () { + + // It fails because either eofbit, failbit or badbit is set. + return 0; +} diff --git a/tie-test.cpp b/tie-test.cpp new file mode 100644 index 0000000..8623316 --- /dev/null +++ b/tie-test.cpp @@ -0,0 +1,24 @@ +#include + +/* + * + * + * + * + */ + +int main () { + + std::ostream *a = &std::cout; // get address of cout + std::ostream *b = std::cin.tie(&std::cout); // Tie cin and cout + std::ostream *c = std::cin.tie(); // return address of that cin is tied to (cout) + std::cin.tie(0); // Untie by giving null + std::ostream *e = std::cin.tie(); // Check if it's tied, should return null + std::cin.tie(c); // Tie it again to address pointed to by c + std::ostream *f = std::cin.tie(); // Check if it's tied. Should return same address as c or &std::cout + std::cout << a << "\n" << c << std::endl; + std::cout << e << "\n" << f << std::endl; + if(b) // compiler please be quiet + + return 0; +} -- cgit v1.2.3