简体   繁体   中英

How to understand the instructions of an istream function?

I am writing a function to the stream and am on the read function to the istream. My ostream function looks like this:

 ostream& Time:: write(ostream& ostr)const {


      int hour;
      int minutes;

      hour = m_min / 60;
      minutes = m_min % 60;

     // Time::write(ostr);
      ostr << setfill('0') << setw(2) << hour;
      ostr << setfill('0') << setw(1) << ":";
      ostr << setfill('0') << setw(2) << minutes;

      return ostr;
   }
 std::istream& read(std::istream& istr);

Reads the time form a stream in H:M format. It makes sure that the two integers (hours and minute) are separated by ":", otherwise it will set the istream object in a failure state. (see the steps below to see how)

Note that this function does not react to any invalid data. It simply follows these steps:

  • reads the integer for the hours using istr

  • reads one character and makes sure it is ':', if it is not is will set the istream object a failure state by issuing this command:

     istr.setstate(ios::failbit);
  • reads the integer for the minutes using istr

Note: Do not clear or flush the istream since this method complies with the istream standards. The caller of this function may check the istream state to make sure that the read was successful if needed.

So far this is what I have but I am not sure if I am getting what it is asking me to do or if I am doing it right:

   istream& Time::read(istream& istr) {
      int hour;
      int minutes;

      hour = m_min / 60;
      minutes = m_min % 60;


      istr >> hour;
      if (hour != ':') {
         istr.setstate(ios::failbit);
      }
      istr >> minutes;

      cout << hour << ":" << minutes << endl;

   }

Am I doing this right? If not, how should I interpret these instructions?

No; your squashed two steps into one, and are therefore missing one read.

The instructions say:

  • reads the integer for the hours using istr
  • reads one character and makes sure it is ':'

Instead, you did this:

  • reads the integer for the hours using istr and makes sure it is ':'

You'll need to introduce another read operation, into a char , and make it so that your check for ':' happens on that char rather than on hours (which makes no sense.).


Furthermore, these lines:

hour = m_min / 60;
minutes = m_min % 60;

…have been copy/pasted from the output function, but make no sense in this context and don't do anything useful. You can remove them.


Also it's weird to cout from within an input function, though it's fine if you're just doing that temporarily for debugging while you're developing it.


Finally, you'll need to return istr , just like you did return ostr before. (Your compiler should have warned you about this?)


Other than that, good work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM