Concepts / Reading Strings From Users

Reading Strings From Users

Input read into a string s does not overflow, according to the source.

  • Programming

Two Questions About Reading Text

When a program reads text from the user into a string, two questions come up quickly. The first is whether the text will fit. The second is what happens to spaces that appear before the text actually starts. This article answers both, using two facts from the source. Keep one question in mind as you read: once the input step is finished, what does the string actually contain?

Where Typed Characters Go

Reading input into a string means that characters typed by the user move from the input stream into the string variable s. A helpful way to picture this is to think of the input as a stream of characters. The >> operator pulls from that stream, one piece at a time, and stores what it keeps into s.

The diagram below shows this movement for a line that begins with three spaces followed by the word hello. The spaces are the part the >> operator handles by default, and the letters that remain are the part that becomes the value of s. The source gives only this teaching model, so the rest of this article sticks to what the source states.

Why the String Does Not Overflow

A common worry with input is overflow: the incoming text is too large for the space set aside to hold it and spills past the end of that space. The source states a direct fact on this point. Input read into string s does not overflow.

How >> Treats Leading Whitespace

The second fact concerns whitespace at the start of the input. Whitespace means the blank characters, such as spaces, that appear before any visible characters. By default, the >> operator skips initial whitespace before reading a value. Because those blank characters are skipped, they are not part of the value that is read.

This gives a practical rule for prediction. When you want to know the value of a string after input, first identify the initial whitespace that >> will skip. Then consider only what remains.

Tracing a Program That Reads a Word

Code walkthrough
#include <iostream>
#include <string>

int main() {
    std::string s;
    std::cin >> s;
    std::cout << s << std::endl;
    return 0;
}
  1. 1
    The string s is declared empty and ready to receive input.
  2. 2
    When the user types three spaces followed by hello, the >> operator skips the three leading spaces.
  3. 3
    The characters h, e, l, l, o are stored in s, so s holds hello without the leading spaces.
  4. 4
    Printing s shows hello, with no leading blank characters.
  • std::string s; creates a string variable named s
  • std::cin >> s; reads from the input stream into s, skipping initial whitespace by default
  • std::cout << s prints whatever ended up stored in s
Output
hello

What do you think happens?

If the user types three spaces and then hello, what does s contain afterward?

  • three spaces followed by hello

  • hello

  • only the three spaces

  • nothing

Reveal answer
Answer:

hello

By default, >> skips the initial whitespace, so the leading spaces are not part of the value stored in s. Only hello remains.

Expecting Leading Spaces to Survive

  • Common mistake

    Expecting the leading spaces typed by the user to appear in s.

    What it looks like

    The user types three spaces followed by hello, and the learner expects s to hold three spaces and hello.

    Why it fails

    By default, the >> operator skips initial whitespace before reading a value, so those spaces are not part of the value that ends up in the string.

    Do this instead

    Predict the value by first removing the initial whitespace that >> skips, then consider what remains, which here is hello.

  • Common mistake

    Expecting a long input to overflow s and be cut short or corrupted.

    Why it fails

    The source states that input read into string s does not overflow.

    Do this instead

    Base your expectation on the stated fact and do not assume an overflow problem for this case.

Predicting Before Reading

EASY

A user types two spaces, then the word code, then presses Enter. Before you check the result, predict the exact value of s after std::cin >> s runs. Explain which characters the >> operator skips and which characters it stores.

Hints
  • Start by identifying the initial whitespace that >> skips by default.
  • Only the characters that are not skipped as leading whitespace can become part of the value in s.

Key Takeaways

  • According to the source, input read into a string s does not overflow.
  • By default, the >> operator skips initial whitespace before reading a value.
  • Leading spaces are therefore not part of the value stored in the string.
  • To predict a string's value, first identify the initial whitespace that >> will skip, then consider what remains.

More concepts starting with R