As we see with nearly all computer programming languages, user input and output are a key component to any program. They often form some of the first things we learn when learning a new language since nearly every program we write will create some type of input or output. In this tutorial, we will explore the basics of user input and output from the console.
Following the customary practice, our first program will simple output Hello World
to the console. Check out the program below and see if you can update it to say hello to Karel.
Let’s take a closer look at this first program in C++ by looking from the inside out. Our basic output comes on line 7 with the cout
command, which stands for console out. Console out is a stream and we can add different pieces to this as we will see later.
The cout
command is found inside the main
function. All C++ programs need to have a main
function and this is where the program starts execution statements. Notice that the return type for the main
function is an int
and at the end of the function on line 9 we return the integer 0. All of this is required for our program, but we will not get into all of the details in this tutorial.
Before our main
function, we have two additional lines. Line 1 is an include statement telling C++ that we want to include the IO Stream library in our program. This is the library that contains the cout
command. Line 3 tells C++ that we will be using the std
namespace. C++ requires program commands to specify the namespace that the command comes from, however we can specify the std
namespace at the beginning of our program so that C++ will apply that to any commands unless we state otherwise. Again, for both of these commands, we will not go into all the details, but be sure to include them for now!
As mentioned above, the cout
is a stream that we can add to. Take a look at the example below where we add a string variable into our output stream.
Notice in the above example how we use the <<
to add additional pieces into our output stream. We can add as many as we want and if we want to go to the next line, we can continue on a new fine.
For example, try replacing line 9 above with the following code:
Did it do what you expected?
It should have had no effect on the output, but why is that? Did you think it would print on three different lines?
In C++, the default cout
command does not go to a new line. In order to go to a new line we need to add an end line command into out console output stream. We do this with a endl
command.
Try it out below.
What if we want to ask the user for their name? Just like output is critical to our programs, user input is also critical. In C++, user input follows a very similar format to our output. Instead of streaming to our console output, we stream the other way with our console input stream.
Notice that the streaming operator is reversed since we are now streaming from our console into our string variable. We create our variable on one line and then stream into it on the next line.
We may also want to provide a prompt for the user so they know what they are inputting. See the example below.
Now it is your turn to try it! Take the example above and add to it. Ask the user for their favorite color and then tell them that something about that color.
For example, maybe your output looks like this: