go to start Ss W6
|home |print view |recent changes |changed October 20, 2017 |
exact
|You are 54.196.47.128 <- set your identity!

Sections: Lucid C++ | Videos | Refresher on Stream States |

Lucid C++ ^

Chapter on defining types and operator overloading: https:files/C++chapter4.pdf

Videos ^

Refresher on Stream States ^

As a little help to guide you through reading the Word(s) from an input stream you can follow the rule of thumb: "do as the ints do". Below you see a piece of code you can copy into a Cevelop project and run directly.

  #include<iostream>
  #include<sstream>
  #include<iterator>
  #include<vector>

  
  std::string bool_value(bool b) {
    return (b ? "true" : "false");
  }

  
  void printStreamState(std::stringstream const &s) {
    std::cout << "s.eof():  " << bool_value(s.eof()) << "\n";
    std::cout << "s.fail():  " << bool_value(s.fail()) << "\n";
    std::cout << "s.bad():  " << bool_value(s.bad()) << "\n";
    std::cout << "s.good():  " << bool_value(s.good()) << "\n";
    std::cout << "bool(s):  " << bool_value(bool(s)) << "\n";
    std::cout << "-----------------------------------------" << "\n";
  }

  
  int main(int argc, char **argv) {
    std::stringstream s { "23 5" };
    printStreamState(s);
    int i { };

  
    s >> i;
    std::cout << "value after first read: " << i << "\n";
    printStreamState(s);
    s >> i;
    std::cout << "value after second read: " << i << "\n";
    printStreamState(s);
    s >> i;
    std::cout << "value after third read: " << i << "\n";
    printStreamState(s);
  }

As a result you'll see the contents of the variable i after each read. After the contents of i you see the state of the input stream s. For lazy students here is the program output:

  s.eof():  false
  s.fail():  false
  s.bad():  false
  s.good():  true
  bool(s):  true
  -----------------------------------------
  value after first read: 23
  s.eof():  false
  s.fail():  false
  s.bad():  false
  s.good():  true
  bool(s):  true
  -----------------------------------------
  value after second read: 5
  s.eof():  true
  s.fail():  false
  s.bad():  false
  s.good():  false
  bool(s):  true
  -----------------------------------------
  value after third read: 5
  s.eof():  true
  s.fail():  true
  s.bad():  false
  s.good():  false
  bool(s):  false
  -----------------------------------------

Details about the stream flags can be found here (with a nicely colored table at the bottom): http://en.cppreference.com/w/cpp/io/ios_base/iostate


|home |print view |recent changes |changed October 20, 2017 |
exact
|You are 54.196.47.128 <- set your identity!

Ss W6
go to start