Concepts / Writing Output with the << Operator

Writing Output with the << Operator

The >> operator reads data from the standard input stream cin into the variable on its right.

  • Programming

A Stored Number Nobody Expected

A program that needs data from the user has to pull that data from somewhere. In C++, the place it pulls from is the standard input stream, and the tool that moves data out of that stream and into a variable is the >> operator. This article starts from a concrete question rather than a definition: when a program reads a number into a variable, what decides which value ends up stored there, and how would you find the line responsible if the stored value were wrong?

What do you think happens?

The program declares an int named count and a double named price, then runs cin >> count; and then cin >> price;. The user types 42 and then 3.7 on the same line. What do the two variables hold afterward?

  • count holds 42 and price holds 3.7

  • count holds 4 and price holds 2.37

  • count holds 42 and price holds 42.0

Reveal answer
Answer:

count holds 42 and price holds 3.7

Each >> read takes the next whitespace-separated piece of input and parses it according to the type of the variable on the right. The int target parses 42 as an integer, and the double target parses 3.7 as a floating-point number.

Before reading the rest of this article, write down your own prediction for each variable. Then compare it with the trace below. The gap between your guess and the trace, if there is one, shows you exactly where your mental model and the program's behavior disagree.

Reading the Statement from Left to Right

Read the statement cin >> variable from left to right, the way the operator suggests. The name cin is the standard input stream and acts as the source. The operator >> is the transfer. The variable named on the right is the target. The stream is always written on the left and the destination on the right.

This layout runs in the opposite direction from what many beginners expect. Beginners often picture assignment as flowing from right to left, so they read cin >> count as if count were the source. It is the other way around: the data comes out of cin and lands in count.

The path of a single read has four parts. Characters arrive on the standard input. The cin stream holds them. The >> operator takes what the stream provides. The target variable is where the result is kept. The variable is the only part of this path that changes as a result of the read.

Typed inputStandard input stream(cin)Operator >>Target variablecharacters arrivestream provides dataparsed value stored
Where does the data come from, how does it travel to the variable, and what is left behind in the input stream after the read? Only the target variable changes as a result of the read.

How the Target Type Decides What Is Accepted

The most important fact about >> is that the type of its right-hand operand determines two things at once: what kind of input is accepted, and what the target of the input operation is. The operator does not have one fixed behavior. It reads according to the variable you give it. The same statement form, cin >> something, can read an integer or a floating-point number depending on how something is declared.

When you are debugging a read, the declared type of the variable is the first thing to check, because it governs what the >> operator will attempt to accept. The right-hand operand is the branching point: an int target leads to integer input, and a double target leads to floating-point input.

int countAccepts 1234integer inputdouble priceAccepts 12.34e5floating-point inputreadsreads
How does the declared type of the target variable change what the same >> statement accepts? The operator and the stream are identical in both columns; only the target type changes.

Tracing Two Reads in Sequence

The source gives a concrete pair of reads that combines both target types. Work through them as a trace, one line at a time, asking at each step what the operator is reading from and what it is writing into. The two reads share the same operator and stream; only the target changes.

cpp
Output
With the input line 42 3.7 the program prints: 42 3.7
Notes on the code
  • Line 4 declares the int target
  • Line 5 declares the double target
  • Line 6 reads the first token, 42, into count
  • Line 7 reads the next token, 3.7, into price
  • Line 8 prints both stored values
Worked example

Following the Input 42 3.7 Through Two Reads

The stream holds the characters 42 3.7 when the program reaches its first read. What does each variable hold after each line runs?

  1. 1
    Declare the targets

    count is an int and price is a double. Neither has a value yet, so no input has been stored.

  2. 2
    First read: cin >> count

    The operator takes the first piece of input, 42, and parses it as an integer because the target is an int. The value 42 is stored in count. The remaining characters, 3.7, stay in the stream.

  3. 3
    Second read: cin >> price

    The operator takes the next piece, 3.7, and parses it as a floating-point number because the target is a double. The value 3.7 is stored in price.

  4. 4
    Check the stored values

    count holds 42 and price holds 3.7. The first place a surprising value could appear is the line whose target type does not match the input you typed.

Result

count holds 42 and price holds 3.7.

Buffer: 42 3.7Buffer: 3.742 consumed into countBuffer: empty3.7 consumed into pricecin >> countcin >> price
After each cin >> read, which characters have been consumed from the input buffer and which remain for the next read?

What Happens to Input That Is Not Read

Each read consumes only the part of the stream it needs. A read that succeeds removes its piece of input, and the next read starts from whatever is left. This is why the input stream behaves like a source of tokens that is used up over time, not like a value that stays in place.

cpp
Output
With the input line 42abc, count receives 42. The characters abc are not a valid floating-point number, so the second read does not store 3.7 or any other meaningful value in price.
Notes on the code
  • The first read takes the leading 42 and stops at the letter a
  • The unread text abc remains in the stream
  • The second read starts at abc, which does not match a double target

Mistakes Beginners Make with the Read Direction

Most errors with the >> operator come from reading its direction or its target incorrectly. The following mistakes are the ones to check first when a read seems to produce the wrong result.

  • Common mistake

    Reading cin >> count as if count were the source of the data

    What it looks like

    Thinking that the value flows from count into cin

    Why it fails

    The stream is always on the left and the target on the right, so data moves from cin into the variable.

    Do this instead

    Read the statement left to right: the stream is the source, >> is the transfer, and the variable is the target.

  • Common mistake

    Assuming the operator behaves the same no matter what the variable is

    What it looks like

    Expecting 3.7 to be accepted by an int target in the same way it is accepted by a double target

    Why it fails

    The type of the right-hand operand determines what input is accepted, so the same statement form parses input differently.

    Do this instead

    Check the declared type of the target variable first, because it governs what the read will attempt to accept.

  • Common mistake

    Forgetting that unread input stays in the stream

    What it looks like

    Expecting the second read to receive the first token again

    Why it fails

    Each read consumes the input it uses, and the remaining characters are left for the next read.

    Do this instead

    Trace what each read removed from the stream and what it left behind.

Finding the Line Where a Trace Diverges

When the trace for a program disagrees with your prediction, the goal is to locate the statement where the two first part ways. For reads with the >> operator, the most useful sequence is to check the target declaration, then the read statement, then the value the variable holds afterward.

MEDIUM

A program declares int count; and double price;. The user types 7 2.5 on one line. The program runs cin >> price; first and then cin >> count;. Predict what each variable holds afterward, then identify the first line where your prediction could differ from the actual behavior.

Hints
  • Start with the declaration of each target and note its type.
  • Ask which token the first read takes from the stream, since the first read is now the one into price.
  • Consider what remains in the stream for the second read.

Key Takeaways

  • The >> operator reads data from the standard input stream cin into the variable on its right.
  • The type of the right-hand operand determines what input is accepted and how it is parsed, so an int target accepts integer input such as 1234 and a double target accepts floating-point input such as 12.34e5.
  • Read the statement left to right: cin is the source, >> is the transfer, and the variable on the right is the target.
  • Each read consumes the input it uses, so the next read starts from what remains in the stream.
  • When a value is surprising, check the target declaration, then the read statement, then the stored result, in that order.

Related concepts

More concepts starting with W