In the previous lessons, we used std::cout to print strings to the screen. Now, let's explore how to read values from the user using std::cin and examine performance hacks when printing to the console (because no one likes slow applications).
Reading Input with `std::cin`
Just as std::cout represents the standard output stream (the screen), std::cin represents the standard input stream (the keyboard).
To read input, we use std::cin along with the extraction operator >>. Think of the arrows as pointing to the right, showing that data flows from the keyboard into the variable.
When you run this program, it will pause and wait for the user to type. The extraction operator automatically splits inputs based on whitespace (spaces, tabs, or newlines). If the user types 24 1.82 and hits Enter, 24 is extracted into age and 1.82 is extracted into height.
Handling Stream Fail States (Input Corruption)
What happens if the program asks for an integer age, but the user types a word like 'twenty'? In JavaScript or Python, this might evaluate to NaN. In C++, it corrupts the input stream.
When extraction fails, std::cin enters a fail state (setting a flag inside the stream), ignores all future input requests (your program will bypass all subsequent cin calls entirely!), and leaves the target variable unmodified (or set to 0 in modern C++).
To build resilient code, we must explicitly detect and clear stream fail states:
The Performance Pitfall: `std::endl` vs `\n`
When writing loops or high frequency prints, using std::endl can make your program significantly slower. This is a very common performance mistake that makes senior engineers twitch.
To understand why, you must understand how operating systems write text. Accessing the physical console is slow. To speed things up, the OS stores characters in an internal memory buffer first, and prints them in batches.
As I mentioned earlier, std::endl does two things:
1. Inserts a newline character (\n) into the output stream.
2. Flushes the stream buffer (forces the OS to immediately write the buffer contents to the screen).
Flushing the buffer forces a slow system call. If you are printing 100,000 lines of data and use std::endl on each line, your CPU will spend most of its time waiting for the physical output to complete. It's like calling a delivery driver to deliver one french fry at a time.
Instead, if you use the character \n (or "\n"), C++ will buffer the output. The buffer will only flush when it fills up or when the program finishes, making execution drastically faster. It's like delivering the whole meal in one trip.
Competitive Programming Speed Hack
If you are ever writing code that processes millions of lines of input and output, the C++ standard library syncs itself with C's stdio library by default to allow mixing printf and cout. This synchronization adds overhead.
You can disable this synchronization at the top of your main function to make std::cin and std::cout just as fast as raw C functions:
Only use this speed hack when you are sure you won't be mixing C style I/O functions (like printf and scanf) with C++ stream functions.