Overview
In the world of programming, communication with the user or other parts of a program is crucial. You need to display information to the user and gather input from them. This chapter is all about handling input and output in C++ using the iostream
library. We will explore cout
, cin
, and endl
, three fundamental tools for achieving this.
The input/output library
The input/output library (io library) is part of the C++ standard library that deals with basic input and output. We will use the functionality in this library to get input from the keyboard and output data to the console. The io part of iostream stands for input/output.
To use the functionality defined within the iostream library, we need to include the iostream header at the top of any code file that uses the content defined in iostream, like so:
#include <iostream>
// rest of code that uses iostream functionality here
Output with cout
The iostream library contains a few predefined variables for us to use. One of the most useful is std::cout, which allows us to send data to the console to be printed as text.
Imagine you are writing a program, and you want to display a message to the user. In C++, you can do this using the cout
(pronounced “see-out”) object. cout
is short for character output
.
Here's a simple example of how you can use cout
to display a message on the screen:
#include <iostream> // for std::cout
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
In this example, we include the <iostream>
header to gain access to the cout
object. Then, we use the <<
operator (which is known as the stream insertion operator) to output the text “Hello, world” to the console. The << std::endl
part is used to add a newline character, making the output more readable.
You can also use cout
to display the values of variables:
#include <iostream>
int main() {
int age = 25;
std::cout << "My age is: " << age << std::endl;
return 0;
}
Input with cin
std::cin
is another predefined variable that is defined in the iostream
library. Whereas std::cout
prints data to the console using the insertion operator (<<
), std::cin (which stands for “character input”), reads input from keyboard using the extraction operator (>>
). The input must be stored in a variable to be used.
Just as cout
is used for output, cin
(pronounced “see-in”) is used for input. You can use cin
to read data from the user via the keyboard.
Here's an example of how to use cin
to get the user input:
#include <iostream>
int main() {
int age;
std::cout << "Please enter your age: ";
std::cin >> age;
std::cout << "You entered: " << age << std::endl;
return 0;
}
In this program, we declare an integer variable age
, prompt the user to enter their age using cout
, and then use cin
to read the value entered by the user into the age
variable. Finally, we use cout
to display the value back to the user.
endl
for Line Endings
You might have noticed the use of std::endl
in the previous examples. endl
is a manipulator in C++ that is used to insert a newline character ('\n'
) and flush the output buffer. In simpler terms, it moves the cursor to the beginning of the next line and ensures that any output is immediately displayed on the screen.
Here's a quick example to illustrate the difference between '\n'
and std::endl
:
#include <iostream>
int main() {
std::cout << "Line 1" << std::endl;
std::cout << "Line 2\n";
std::cout << "Line 3" << std::endl;
return 0;
}
The output of this program will be:
Line 1
Line 2
Line 3
As you can see, both std::endl
and '\n'
can be used to create newlines, but std::endl
also flushes the output buffer.
std::endl vs ‘\n’
Using std::endl can be a bit inefficient, as it actually does two jobs: it moves the cursor to the next line of the console, and it flushes the buffer. When writing text to the console, we typically don't need to flush the buffer at the end of each line. It's more efficient to let the system flush itself periodically (which it has been designed to do efficiently).
Because of this, use of the \n
character is typically preferred instead. The \n
character moves the cursor to the next line of the console, but doesn't request a flush, so it will often perform better. The \n
character is also more concise since it's both shorter and can be embedded into existing text.
#include <iostream> // for std::cout
int main()
{
int x{ 5 };
std::cout << "x is equal to: " << x << '\n'; // Using '\n' standalone
std::cout << "And that's all, folks!\n"; // Using '\n' embedded into a double-quoted piece of text (note: no single quotes when used this way)
return 0;
}
// Output
x is equal to: 5
And that's all, folks!
When ‘\n’ is used by itself to move the cursor to the next line of the console, it should be single quoted. When embedded into text that is already double-quoted, additional quotes aren't needed.