In this video, we'll talk about stringstreams.
Stringstreams allow us to use in-memory string objects as streams.
That means that we can read and write data to strings in the same way that we can
read and write data to any stream.
This is a very powerful concept, and it's very useful for data validation.
It's also very easy to use since we already know how to use streams.
There are three classes that we can use when using stringstreams.
The stringstream class allows us to read and write from stringstreams.
The istringstream class allows us to read from stringstreams.
And the ostreamstring class allows us to write to stringstreams.
For all of these, we must include the sstream header file.
The rest is very much like using any other stream.
First, we declare a stringstream object of one of the three types that I mentioned,
then we connect it to a string.
That's it. Now we can read or write to that stringstream using formatted io and
stream manipulators if we wish.
Let's see an example of how we could read from a stringstream.
Suppose we have three variables declared: an integer named num, a double name total
and a string called name.
Now suppose that we have string info with the string Moe 100,
1234.5 as its contents.
Remember, we did this example in this section and that same data was in a file.
Well, now it's in memory in a string.
We can create an istringstream object. We'll call it iss
and connect it to the info string.
So now we can read from the stringstream.
Now we can use the stream extraction operator to extract those data from the string.
That's pretty cool. I'm sure you can see lots of applications for this,
especially in data validation.
So now let's see how we can write data to a stringstream.
In this case, we've initialized our variables: num total and name
to 100, 1234.5 and Moe, respectively.
We can create an ostringstream object named oss,
and we can write to the stream object.
In this case, I'm inserting the three values onto the stringstream.
Remember, we can use any stream manipulators to format that data that we're putting on the stream.
So now how do we display the string that we just formatted.
Notice that notice in this case we didn't connect the ostringstream to any string object,
we could have just like we did with the istringstream example in the previous slide.
The stringstream has a string buffer that it uses internally,
and we can use that.
So if we want to display that string, all we have
to do is call the stringstreams.str method.
In this last example, we'll do some basic data validation.
The idea is that I want to ask the user to enter an integer.
And I want to be sure that they do enter an integer, not some invalid data like a name.
So we have a string named input,
and we read the user's input into that string.
We don't read it into an integer. Again, we read it into a string.
Then what we want to do is to try to convert that string to an integer if possible.
There are many ways to accomplish this in c++, some are better than others.
Let's do it with a string stream it's pretty easy.
What we can do is we can create a string stream object, we'll call it ss.
And we can connect it to the input string that the user just entered.
Then we can try to extract an integer from ss
and store it in value.
We can do that using the stream extraction operator.
If the extraction was successful
that means that we have a valid integer stored in value.
If the extraction was not successful,
we know that the user didn't enter an integer,
and we can ask them to enter the number again
or we can do whatever else makes sense in our program.
So now let's head over to the IDE, and we'll see these examples,
and we'll do a more complete data validation example as well.
Okay. So I'm in the IDE. I'm in the section 19 workspace in the stringstream project.
And what we'll do here is we'll do a few examples using stringstreams.
We'll read from them, we'll write from them.
And then I'll show you a little bit better data validation example
than the one in this slide, a little more complete.
So what we're doing here we've got some includes, and we're including limits.
And I'll show you what that's about later.
But right now we have iostream, iomanip,
our sstream for our stringstreams and the string class.
Okay. So here's what we've got. We've got these
three variables, just like we saw on the slides:
num is an int, total is a double and name is a string.
And we've created this string right here called info that just has those three
pieces of data in there. It's all a string right now.
But what we want to do is we want to be able to read that or extract that information
from the string as a string,
an integer and a double and store them into these variables right up here.
Yes. it's really easy to do. All we have to do is create
an istringstream, we'll call it iss,
and we're going to hook it up to that string info, this guy right here.
And now what we can do is we can extract information from iss,
which is a stream the same way that we extracted information from cin,
the same way we extracted information from a file, same idea.
Remember, we're using the stream abstraction,
and we really don't care what the stream is connected to.
In this case, it's connected to an in-memory string.
And that's what we're doing here, we're reading name, num and total.
The compiler knows the types for these guys because they're defined right up here.
So it's going to try to read a string
from the string iss, which is connected to this guy right here.
And it's going to grab Moe and store it in name.
Then it's going to get 100 stored in num, and it's going to get 1234.5
and stored in total we'll display this information out to the
console right here with cout.
And we'll get a nice formatted output. So let's see what it looks like.
And there you go. Moe 100 and 123.45.
That's the data that was in this string.
So you can see how easily we really what we were doing is actually parsing that string
and parsing information out of there typed, which is kind of cool.
And it was so easy to do we really had to do very little.
All we had to do is this one line 17 right here. Once we hooked up the stringstream.
Okay. So that's an example of input. Let me show you an example of output.
I'll comment this piece of code out or uncomment that, I should say.
Notice what we're doing here, we're going to use an output stringstream here
and we'll call it oss. And we're not going to hook it up to anything
behind the scenes the streamstream class has its own string buffer
and that's what we're going to use.
So in this case, what we're doing is we're instantiating oss right here.
And then we're just going to write to the stream,
and the stream is going to store that information in that string buffer that it has.
So what we're going to do is we're going to display name,
we're going to display num and we're going to display total. We still have those from this piece of code up here.
And we're going to display them using these stream manipulators.
And then when we're done we want to display the string, right.
But all we have is that stream oss. I know string and stream sound alike,
it's hard for me to say them because I confuse them myself. So
all we have is a stream oss. It's connected to a string.
So what we want to do is display the string.
And the way we get to that string is we use the str method
for the stream and that will return that internal
string buffer that it's got it'll display it. So what we're doing here is
we're we're outputting all of this information
to that stream
which is connected to its own string and then we're displaying the string.
So when we display the string, you should see this display in a formatted manner.
So let's try that.
There it is, Moe 100 123.45. It's the same
formatting that we used right up here.
Makes sense. So what we're doing is we're displaying this. Remember, before it went to c out
now it's not going to see out. Now it's going to that stringstream.
They're both streams right, cout is an output stream.
And oss is an output stream. It just happens to go to a string.
And now let's do an example for data validation.
You guys had a lot of questions about data validations on the on the forums.
So I decided to do an example using the stringstream.
All right. So let's go through this one really slowly here.
This is the value. At the end of this, I want the
user to enter an integer, and I want that integer to be in that
value variable right here. Okay.
But I can't -- you've seen how we can't just ask the user
for an integer and then do something like cin,
so we could read the console input into that value variable.
That doesn't work real well, right. When they type strings and when they type
just silly stuff on the keyboard, it's going to fail.
So we've got to have a better way to do that.We have to be able to validate
the type that they're entering. So that's what we're going to do here.
Rather than read directly into an integer, we're going to read into a string, right here entry.
Okay. And we're going to do this in a loop, and we're going to keep doing it until they enter something that's valid.
So let's talk about the loop
what we'll do what we'll do first is we're going to create a Boolean
flag right here
called done.
And we're going to set done to false because we're not done.
We're done when we're done, right. And we're starting out -- obviously,we're not done when we first start.
So we've got a do loop or do while loop here.
And what we're going to do first is we're going to say
hey user please enter an integer and we're going to read the integer into entry.
Remember, entry is that string.
So now we're allowing them to enter anything they want, right.
We're going to grab a string, so they can type in numbers letters decimals whatever they want.
And what we'll do is we'll try to validate that to make sure that what they typed in
is an integer. This is where the stringstream comes in,
and we're going to use an istringstream. Here we could have just used a regular stringstream,
it doesn't really matter because it goes both ways, but this is an input stringstream.
And we're going to call it validator, could have called it anything you want.
Typically, you see these things called ss out there in real code.
I don't know why but it's very very common to see stringstreams named ss.
So I'll call it validator, and I'm hooking it up to entry.
So now remember entry is --
suppose entry has -- suppose the user entered one.
Instead of the number one, they entered the string one.
What we're doing is we're hooking up this string, right, input --
or sorry, entry right here to that validator stringstream.
And then this is the key right here.
I'm going to try to extract value what's value an integer,
I'm going to try to extract a value from the stream.
There's no way, right. We've got a string here that's o
and e, that's not an integer. This will fail.
If this fails, we're going to say sorry that's not an integer.
Done is still false and we're going to loop again and I'll talk about this statement here in a second.
But now suppose that they do enter a valid integer. Suppose they enter
123 remember, it's a string.
They're entering strings, but that's a valid integer in that string.
When we get to here and we try to extract that integer from
that entry that they put in through the stream,
we're going to get back 123,
and that's going to be stored right in value. So that's going to be perfect.
This will pass. We'll set done to true,
and we're looping while, we're not done, so we're done,
right. Our loop will finish, and then I believe there's a statement down here at the bottom
that says you enter the integer value, and I'll run this for you in a minute.
Now let's talk about this statement right here.
This is something I hadn't talked about yet. And this these are the more advanced
edge cases and nuances of c++.
Suppose that the user entered something like
12.38. Okay.
Well, this right here will extract the 12,
and 12 will be put into value.
But then we have all this stuff left over on the stream.
So the next stream read doesn't really know that that's there.
So what we're doing here is we're saying
std cin, and we're using cin here because that's where we're reading data from.
We're going to ignore
everything in that stream up to the new line,
and that's what we're doing here, and that's where that limits header file comes in.
So we're using std numeric limits up to whatever the std stream size is,
and that's dependent on your platform.
So however big that stream is the maximum number
I'm getting rid of all of this stuff, I'm just ignoring all that I'm, clearing out that stream.
So that the next time i read from the stream, it's clean and there's nothing left over.
Okay. Because remember, the user could have typed in
12 hello there,
right.
And that space right there
grabs that 12 and that 12 goes into value, this passes. This is true.
So we've got an integer. They did type in an integer.
They typed in a bunch of other stuff as well.
But what we can do is we can grab the 12 and just delete that rest of that stuff. So the next time we read,
we're clean. All right. So let's run this example
and please enter an integer 12.
You entered integer 12. Good, that was an integer that's what we expected.
So let's run this again, please enter an integer let's say I typed in Frank.
Sorry, that's not an integer. How about 0.01,
sorry that's not an integer. But notice now that when I loop around,
whatever was left in that that input stream is gone because I
ignored it, I got rid of it all. So that really helps us with future input.
So now if I did something like 10 Frank is
a good teacher,
something like that,
you entered 10. Notice it grabbed the integer but then it just discarded everything else,
right. So we're ready to read again. And our stream will be in a
in a known state and not some unknown state.
All right. So that's it. I hope you enjoyed this
uh this section on stringstreams. They're very, very powerful.
They're not used that often out there. But sometimes you'll see them used, and
they're used a lot in gui work sometimes when you want to display strings to gui,
graphical user interface elements, we tend to format them in memory and then display them out.
That's it. So let me know if you have any questions about this.