Input/Output in C++
Contents
Standard output
You've actually already seen out we can output/print/display values in C++—we use an object (we'll get to what an object is later) called cout
. The cout
object can be given values, each one separated by two left angled brackets <<
. This displays the values to the CLI. We call this the standard output, or stdout. Here's an example of a program that displays the values of a couple of variables:
This should print: Name: Hank; age: 5
followed by a new line.
Let's take a look at how it looks in a terminal, supposing we named this outputExample.cpp
:
$ g++ outputExample.cpp -o outputExample $ ./outputExample Name: Hank; age: 5
If you need a tab, you can use the special escape character \t
. This can be added directly in a string. For example, cout << "Name: " << name << ";\tage: " << age <<endl;
will add a larger space between Name: Hank;
and age: 5
.
Standard error
Using cout
for output is perfect most of the time. However, if you are printing an error message, the convention is to print it out using standard error, or stderr. This can be done with the cerr
object, which you use exactly the same way as cout
. In fact, in this class, we won't actually see a difference. The reason it is helpful in general is that on a CLI, you can redirect stderr and stdout separately. For example, you can direct all error messages to a log file.
Here's an example of using both cout
and cerr
in a program:
Standard input
So now we can output values so a user can see them, but how do we get input from a user? We will use standard input (stdin), which in C++ is done via the cin
object. The cin
object looks very similar to the two output objects, but we will use right angled brackets and will be reading values from cin
into the variable on the right. Here's how we can read in an age and name (separately) and then print them out:
Note that I added a space after the :
and didn't bother with an endl
—this is so that the prompt shows up on the same line as where the user enters the information, and the extra space prevents the cursor from starting directly adjacent to the :
. It's not required, but it does make the prompt look nicer, so I strongly encourage you to do the same. Here's what it looks like when I compile and run (supposing I've saved it to a file named inputExample.cpp
), with my input in orange:
$ g++ inputExample.cpp -o inputExample $ ./inputExample Please enter your age: 5 Please enter your name: Hank Name: Hank; age: 5
In this example, we read the names in separately. The program will first prompt the user for an age. The user can enter whatever they like, and then press enter on the keyboard. This is important: only the first space-separated value will be read into the variable to the right of >>
. If I entered 15
, then age
would be set to the value 15. If I entered 15 16 17
, age
would still be set to 15, since it's the first of the three white-space separated numbers entered.
This has repercussions for strings with spaces: only the first word will be read in. We'll learn how to read in whole lines in a bit.
We can also have users provide multiple pieces of information on the same line, separated by whitespace, and read them all in at once. Here's how we might read in the age and name of our user all at once:
Here's what it looks like when we run it (again, input in orange):
$ g++ inputExample2.cpp -o inputExample2
$ ./inputExample2
Please enter your age and name: 5 Hank
Name: Hank; age: 5
A helpful way to think of the >>
operator is like asking stdin to get the next thing from its input and store it in the variable to the right. In the example above, the input stream look like: 5 Hank
. The first part of our cin
line looks like: >> age
. This gets the next ting from the input stream, 5
, and assigns it to age
. When we do >> name
, that reads the next thing, "Hank"
, and assigns it to name
.
To read in a full line—everything up to the new line—we need to use a special function called getline
. It works on arbitrary input stream, not just cin
. It take two parameters: the stream to read from (in our case, cin
), and the string variable to put the value in. Note that you cannot read a line into a non-string variable. Here's an example:
Supposing we save this into a file called getLineExample.cpp
and run it, here's what we get:
$ g++ getLineExample.cpp -o getLineExample
$ ./getLineExample
Please enter your full name: Hank Feild
Name: Hank Feild
If you use cin
prior to using getline
, be sure to use cin.ignore()
to remove the newline from the input stream (cin
leaves new lines in the stream).