 Hey, everybody. It's Brian. And in this video, we're going to do something a little different. We're going to do a viewer request. I don't do these often because I get so many of them. But this one did come out of our Facebook group, which is called Void Realms. If you're on Facebook, definitely look that up. We've got 4,000 and growing developers. The question it was more of a demand was, please show the cute way of doing user input and output. Oh boy. This is going to just spawn a holy war in the comments down below. I'm going to show you maybe not the cute way of doing it, but the most convenient way that I've found working with cute and that is using QtecStream. Yes, you can 100% use the standard library. You can use CN and see output. I think QtecStream, in my humble opinion, is a way better way to go, especially when working with cute applications. So why QtecStream? The QtecStream class provides a convenient interface for reading and writing text. Okay, maybe the marketing department could jazz that up a bit. It's not to you really start working with cute that you understand the power of this class. I use it constantly. Pretty much any time I use any text input or output, I'm using QtecStream. There's a counterpart to this called Qdatastream. This is not a complete video on QtecStream or Qdatastream. We're going to do those in separate videos. We're just going to show you at a high level how to use this thing. Here's where the power really comes in. QtecStream can operate on a QIO device, a Q byte array or Q string, and we've covered Q byte array and Q string, the two powerhouses inside of Q core. But there's a third we haven't really touched on yet called the QIO device. But to continue on using QtecStream streaming operators, you can conveniently read and write words, lines and numbers. That's right. You can do that. How do I read multiple lines? How do I read a text file line by line and things like that? And for generating text, QtecStream supports formatting options, keyword formatting. So you can actually do some really cool things. It's all baked in. Rewind real quick, though. QtecStream can operate on a QIO device. What is a QIO device? It's the base interface class of all IO devices in Qt. That's right. Everything pretty much boils down to an IO device at some level. Your mouse, your keyboard, your monitor, different processes, CPU, things like that. It all pretty much boils down to an IO device. And you can see it's used by everything from sockets, serial ports, Bluetooth, buffers, code replies, file devices, files, network replies, processes and so on. I mean, just pretty much any type of device you want to work with at its core is going to use a QIO device, which is why I absolutely love working with QtecStream because it doesn't matter if it's IO from a keyboard or IO from a file system or from a socket, you can still use this. Let's dive in and take a look. Okay. First thing we need to do here is we're going to actually use QtecStream. So let's go ahead and include it. Now that it's included, we can go ahead and let's make an instance of it. So I'm going to say QtecStream and you can name this whatever you want. I'm just going to say Q in short for input. And now we have some options. We can either do a QIO device, we can just make default one, a byte array, pointer to a byte array, QStrang and a file handle. Notice that it's a pointer to a file. So what we can actually do is say we want the stdn, which is short for the standard input. And that's right, we're just going to wrap the system standard input right into a QtecStream. And we can take this pattern here. Call this Qout. And then we can wrap it right into the std or standard output, which of course is just a pointer to a file. So now you have your input and output. This is just ridiculously simple, how you can just wrap that right in. You don't have to do any weird overloading or anything like that. Okay. The first hurdle we have to cross is just simply outputting something to the screen. And this is where a lot of people get hung up and they just get really frustrated. So we're going to say Qout, which of course is our output text stream. See, we got our standard output. We're calling it Qout. And we're just going to simply say something like this, please enter your name. Now, if we simply do this and run it, what's going to happen? You're going to get very frustrated very quickly, because absolutely nothing happens on the screen. And then you're jumping into forms going, wait, why isn't this working? And I see literally thousands and thousands of questions on this. You need to do one of two things, flushing, which we'll talk about later, or end L. And yes, I did say the word flushing, if you've never heard that term before. Now there's two different end L's. There's end L, which if you're in the std or the standard library, there's the standard library way. And you notice we're getting a undeclared identifier, because we don't have the standard library included. Now, before we get too far along here, end L in the C++ reference, its behavior is equivalent to calling the OS put, and then we have the slash n, or basically we are escaping n or making a new line, or OS put OS widen slash n for a character other than the care and then OS flush. What? That's confusing. So basically, every operating system has an end of line character. It's usually slash n, but Windows does treat things, of course, differently because it's Windows. All right, so what we're going to do is we're going to say QT and L. And end L stands for end line. And if you highlight that and hit F1, here's the Qt documentation for the QTECH stream, Qt and L writes a slash n or new line to the stream and flushes the stream. There's that word flushing again, we're going to talk about that in a minute. So it does something like the stream, do a new line, and then Qt flush. Note on Windows, all the n characters are written as if they are Rn, because that's the Windows way of doing it. That's the frustrating thing about cross platform development. So if you're just a normal C++ developer, end L is evil, and I almost always use the Qt end L. Now, this function was introduced in 5.14. So this is fairly new. That being said, I absolutely love it. It makes life so much simpler than trying to figure out which operating system you're on and then sending the appropriate end L. But now, when we run this, something magical happens. It automatically knows, ta-da, something should be put out in the console. And it's because we put a new line, so you see the cursors down here and it flushed the data. Now that we know how to output something, how do we input something? And notice I have this as reading. So what we're going to do is we're going to read, and I'm going to say Q string, name. We've already said to the user, please enter your name. So now the user is expecting, hey, if I type my name, this program is going to do something with it. So we're going to take our input Qtxtream or QN, and we're going to go ahead and read. Now, if you're from C++ land, this looks very foreign, but in Qt, this is perfectly fine. So we're going to say dot, and then read. And notice all these different reads we got here. Read with a maximum length, returns a Q string, read all, that's right. If this was on a file, it would read the entire file back as a string. Read line, which will read up until the end of line or an end L. And then you have all these others. So we're just going to read line. So we're going to read this one line at a time. And if you were paying close attention, that's right, you can do a maximum length to avoid any possible buffer overflows. There are things built into Q string that would keep that from happening anyways, but point being, it's got security in mind, loose security, but security nonetheless. Now, if we go ahead and just do something like this, and we're just going to demonstrate that we actually are able to input and capture that information, save run, it works. It's that magical moment. We're going to talk about flushing. This is often, when I talk about flushing to people that are brand new to this concept, they do giggle because it is exactly what it sounds like. Imagine you have a giant toilet in front of you. Yes, I said it, you have a giant toilet in front of you, and you're going to flush and everything's going to go down. That's exactly what's happening. You're pumping data into this thing, and then you're just going to flush it down to where it needs to go. So we're going to grab this and do a little cut. I love copy and paste, but we're going to cut, and we're going to say, queue out, and then how about something like this? You entered, name, and remember, if we omit the end L, it doesn't output to the screen. It's just simply sitting there. There's a reason for that. If you enter a lot of data outputting to the screen actually takes a little bit of resources and on low end systems, especially embedded systems, if you're doing a lot of data, that will have a time penalty. So if you want to just pump a lot of data out there and then flush it when you're done, that's the correct way to do it. So I'm going to say, enter your age. Now notice what's going on here. It's saying you entered name and then enter your age. If we run this, remember, nothing's going to happen until we flush it. So I'm going to say Brian, and then nothing. So let's fix that. What we're going to do is we're going to flush. Now that we've filled the toilet bowl, pardon the pun, we're going to say output dot flush. And if you remember back from talking about and L and L will actually flush that out. So it will write the slash N or the new line to the stream and then flush it or push all that stuff down to where it needs to go. In this case, the standard output. So let's say Brian, now we have another problem. You entered and then it has our name, but now we have this right here where these are jumbled up together. Let's go ahead and fix that. So what we can do here is say slash N or new line. And if you're on Windows, you could do a slash R slash N or however you wanted to do it. But remember, if you use and L, it will call flush. Now in this simple example, it's really not a big deal. We could just do and L all day long. You're not going to see a speed penalty at all. But if you're going to be writing a lot of data, I would just simply do this. And then when you're done, flush it out. Let's go ahead and put in. And if you wanted to, you could have done an end L here, but we're going to just go slash N here. Now let's run this. Now it's going to ask for our age. The major takeaway from this little segment here is there's a billion different ways to do that. And you may have to fine tune for your specific situation. But in L, we'll put a new line and flush or you can manually put in the new lines and then flush whenever you feel like it. Now that we have all the generalized theory out of the way, let's give a good working example here. So we have gotten to the point where they can enter their name and now they want to enter their age. This is the thing we want to make sure we get a valid number. So I'm going to start with a boolean. I'm going to say bull, okay. And I get this question quite a bit too is how do we get an int. So for example, we can say int age equals Q in for input. And we want to read the line. So what we're getting back here is a Q string. But of course, we want to cast this into an integer. Well, it's baked right in to the Q string, we can say to int. And then we have this pointer to a boolean. I'm just going to give it the address of our okay variable. So what it's going to do in the background is try to cast or convert that string into an integer. And it's going to tell us whether or not it succeeded. And then we can just simply say something like this, if okay, meaning if okay is true, then we can say Q out you are age years old. And we trust now that this is an valid integer because Q was able to cast it for us. Otherwise, we could say else, you have been a naughty person, you don't know how to follow directions. So let's just go ahead and grab this and say you have failed if you enter a valid number. And even give some exclamation points because as programmers, we get very upset when people do that. And then let's go ahead and flush this out. We can say Q out not flush. Different ways you could do that. You could have called and L let's go ahead and run this and see if this thing works. So let's go ahead and enter my name, Brian, and your age, I'm going to say I am 46. You are 46 old 46 years old. Man, I sound old when I say that. Let's go ahead and run this. And let's say my name is Brian. And I am this many years old. You have failed to enter a valid number. All right, major takeaway here is Q text stream makes it extremely simple to work with the standard input and output to the point it's almost just ridiculous. And I feel bad even making this video, but there's a lot of questions and concerns on how to use it. And end L is well, in normal standard library C++ I consider it evil, but in Qt they've actually kind of stream like that a little bit better. And again, that goes back to your version of C++ and so on. But under the hood, this will flush. So if you have long standing operations and you don't want the speed penalty of flushing, just manually enter your inlines and then flush as needed. And of course, you can work with data types, for example, Q string, you can convert that to an integer very, very simply. I hope you enjoyed this video, you can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there, drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.