Explain istringstream

Hi you all,
I have a part of a code that I'm trying to understand. The program lets you put in a start and a end time(hh:mm), and then calculate a price depending on the total time. The part that I don't understand is this. Could someone please explain it as if I were 5(eli5).

1
2
3
4
5
6
istringstream iss(start);
iss >> startHour >> startMinute;
    
iss.clear();
iss.str(end);
iss >> endHour >> endMinute;


This is the other part of the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double cost(string start, string end)
{
    int startHour = 0, startMinute = 0, endHour = 0, endMinute = 0;
    const double moms = 1.25;
    const double prisperMinute = 4*moms;
    
    start.replace(start.find(":"), 1, " ");
    end.replace(end.find(":"), 1, " ");
    
    istringstream iss(start);
    iss >> startHour >> startMinute;
    
    iss.clear();
    iss.str(end);
    iss >> endHour >> endMinute;
    
    //////////////////// calculations
    /// find the total cost and return it
    return minutes(startHour,startMinute,endHour,endMinute)*prisperMinute;
}
What exactly don't you understand?

Did you find and read the documentation for the stringstream class?

What the istringstrem and iss does in this case.
Yes I did, but didn't understand much of that either.
istringstream iss(start);
istringstream object iss constructed with string object start via default istringstream ctor

iss >> startHour >> startMinute;
iss does not read whitespace, so iss until first whitespace is read into int startHour, then whitespace skipped and the second sequence read into int startMinute. If the string start does not match this sequence iss will fail

iss.clear();
done reading, so iss is cleared for next read ... as below ..

iss.str(end);
iss >> endHour >> endMinute;
iss.clear();
done reading, so iss is cleared for next read ... as below ..

No, the clear() clears any error flags, it doesn't erase the stream contents.

This: iss.str(end); sets (copies) the string "end" to be the source for the iss stream.

Yes I did, but didn't understand much of that either.

Then you need to either find more documentation, documentation that you understand, or perhaps change professions. If you can't read and understand documentation for the standard C++ classes you're not going to have much luck becoming a C++ programmer, IMO.

By the way what documentation did you find?

Last edited on
Note that you extract data from std::istringstream much the same way you do with std::cin. The main difference is that you read the input from a string instead.
@jlb: you seem to know a lot about stringstreams, maybe you could have helped OP in the first place rather than giving him/her the run around, specially as the OP had demonstrated personal efforts
specially as the OP had demonstrated personal efforts

Where? It looks like the OP found some code that he doesn't understand and want someone to do the thinking for him.

As I said in my last post if the OP can't understand documentation then he either needs to find other/more documentation (perhpas: http://www.cplusplus.com/reference/sstream/istringstream/ or http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/ ), or find another profession. Being able to find, read, and understand documentation is a very very important part of programming.

I don't want anyone to do the thinking for me. I just haven't found any documentation that is simple enough for me to understand, that's why I ask to get it explained as if i were 5. And @gunnerfunner gave me a good explanation, so thank you for that!
Topic archived. No new replies allowed.