Chapter 9 Input and Output
In this chapter, we will learn how to take in input from the user, and
output/print data to the screen. C++ handles this using the notion of
streams. In particular, we will focus on two standard streams: the input
stream cin
for receiving input, and the output stream cout
for
displaying output. In order to make use of these streams, we must
include the iostream
header file at the start of our program. One
thing to keep in mind while reading this section is that C++ makes input
and output very easy. In particular, we do not need to worry about
whether our input or output is an integer, a float or a string—C++
handles that for us automatically. By contrast, Python requires us to
manually ensure that the input is of the correct type.
We will be using the string
data type in many examples in this
chapter. Because string
is not a built-in data type, we should in
theory need to include it by writing #include <string>
at the top of
our files. However, it turns out that all C++ compilers have iostream
libraries that themselves include string
. Thus when we include
iostream
, we are also conveniently including string
as well.
9.1 Output
The iostream
library contains the cout
stream, which we can use to
send data to be displayed as text in the terminal. Let’s look at our
simple Hello, world!
program again and see what’s going on.
#include <iostream>
using namespace std; //use standard definitions
int main(){ //bracket signals the start of the main function
<< "Hello, world!";
cout return 0;
}
First off, note that we include iostream
at the very beginning so that
we can use the cout
stream. Then on Line 6, we print some text to the
console. We do this by using the cout
stream and the insertion
operator (<<
). Note how the insertion operator is like two arrows
pointing towards the stream. This essentially tell the program to send
what is on the right-hand side (in this case, the text) to the stream,
which then handles displaying it in the terminal.
Note that cout
can print more than just text—it can print numbers
and variables too, as in the example below:
int x = 10;
<< 5;
cout << x; // prints the value of x to console cout
In order to print more than one thing on the same line, we can use the insertion operator multiple times in a single statement to concatenate multiple pieces of output:
= "David";
string name << "Hello, " << name << "!"; // prints Hello, David! cout
By default, C++ does not print a new line at the end of these output
statements. One way to achieve this is to use endl
, which is also
contained in the iostream
library, and forces the stream to print a
new line character. For example:
= "David";
string name << "Hello, " << name << "!" << endl;
cout << "My name is HAL" << endl; cout
In the above example, everything would appear on the same line if we did
not include endl
. The same effect can be had by including the newline
character \n
in the text as so:
= "David";
string name << "Hello, " << name << "!\n";
cout << "My name is HAL\n"; cout
9.2 Input
Having looked at output, input is very similar. In order to accept input
from the keyboard, we now use the cin
stream, which again is defined
in the iostream
header file. Whereas cout
prints data to the
terminal using the insertion operator,cin
reads input from the
keyboard using the extraction operator (>>
). The input must be
stored in a variable to be used, as follows:
int age; //we must create a variable to store input
>> age; // read in data and store in age
cin << "Wow, you are " << age <<"! You old!" << endl; cout
To remember the extraction operator, notice how it looks like two arrows
pointing towards the variable that will hold the input. A fantastic
feature of C++ is that input works the same regardless of the type being
read in. In Python, if we wanted to read in an integer, we would need to
write x = int(input())
. Similarly, a float is read in
x = float(input())
. By contrast, C++ handles this automatically, as in
the example below:
int x;
double y; //recall that double is for real numbers
;
string z>> x;
cin >> y;
cin >> z; // C++ handles the types for us! cin
We can also chain together input statements on a single line to read in multiple values:
;
string nameint age;
>> name >> age; //will read in a string, then an int cin
9.2.1 String Input
An important detail about input in C++ is that it will read all the characters up to the first whitespace it encounters. This whitespace could be a regular space, or a newline, but C++ will only accept input up to this point. For reading strings, this means that C++ only reads one word at a time. Let’s look at this using the example below:
#include <iostream>
using namespace std;
int main(){
;
string firstName>> firstName;
cin << "Hello, " << firstName << endl;
cout return 0;
}
If I were to try enter both my first and last name, separated by a space, only my first name would be read and stored in the variable. My surname would be waiting in the input stream for another read to occur. If we want to accept both the first name and surname, we can achieve this by modifying our program to look like this:
#include <iostream>
using namespace std;
int main(){
;
string firstName;
string surname<< "Please enter your first and surname" << endl;
cout >> firstName >> surname; //input first name and surname
cin = firstName + " " + surname; //concatenate
string fullName << "Hello, " << fullName << endl; //output message and name
cout return 0;
}
If we want to read in an entire line, including any whitespaces, we
can use the getline
function like so:
#include <iostream>
using namespace std;
int main(){
;
string name<< "Please enter your full name" << endl;
cout (cin, name); //reads ENTIRE LINE into name
getline<< "Hello, " << name << endl;
cout return 0;
}
Note however, that mixing getline
and cin
can cause subtle errors,
so try avoid this where possible.
Finally, we present code that asks the user to input their first name and year of birth and then displays the user’s first name and age:
#include <iostream>
#include <string>
using namespace std;
int main(){
;
string firstName<< "Please enter your first name" << endl;
cout >> firstName; //input first name
cin int yearOfBirth;
<< "Please enter your year of birth" << endl;
cout >> yearOfBirth;
cin int age = 2018 - yearOfBirth;
<< "Hello, " << firstName << endl; //output message and name
cout << "Your age is " << age << endl; //output message and age
cout return 0;
}
9.3 Summary Lecture