C++ noskipws Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, noskipws turns off the habit of >> to skip whitespace. After it, spaces, tabs, and new lines are read just like letters.

It is the easy way to read text character by character with >> and keep every space. It comes from <ios>, which <iostream> includes.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::noskipws into an input stream before reading characters.

C++
char c;
std::cin >> std::noskipws;
while (std::cin >> c) {
    // c can be ' ' or '\n'
}
CallWhat it does
in >> std::noskipwsWhitespace is no longer skipped.
in >> std::noskipws >> cReads the very next character into c.
in >> std::skipwsTurns skipping back on.

For a single character, in.get(c) gives the same result without changing any setting.

Library declarations
C++
ios_base& noskipws(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cin >> noskipws. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after noskipws.

C++ Examples

Example 1 — Count the Spaces in a Text

Read every character, including spaces, and count the spaces.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("a b  c");
    char c = 0;
    int spaces = 0;

    in >> std::noskipws;
    while (in >> c) {
        if (c == ' ') {
            spaces++;
        }
    }

    std::cout << "spaces: " << spaces << '\n';
    return 0;
}

How It Works

  1. noskipws makes in >> c return every character, spaces included.
  2. The text has one space after a and two after b, so the counter reaches 3.
  3. With the default skipws the spaces would be skipped and the counter would stay at 0.

Example 2 — Copy Text with All Its Spaces

Read character by character and write each one to a new string stream.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("hi there");
    std::ostringstream out;
    char c = 0;

    in >> std::noskipws;
    while (in >> c) {
        out << c;
    }

    std::cout << out.str() << '\n';
    return 0;
}

How It Works

  1. Each in >> c reads one character. With noskipws the space in the middle is a normal character.
  2. out << c writes the character to the second stream, one by one.
  3. The copy is hi there, with its space kept. Without noskipws you would get hithere.

Example 3 — See the Space After a Number

Read a number, then check the very next character.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("7 8");
    int n = 0;
    char c = 0;

    in >> n;
    in >> std::noskipws >> c;

    std::cout << "n = " << n << '\n';
    std::cout << "next char is " << (c == ' ' ? "a space" : "not a space") << '\n';
    return 0;
}

How It Works

  1. in >> n reads 7 and stops at the space. The space stays in the stream.
  2. With noskipws the next >> does not skip that space, so c receives it.
  3. The test c == ' ' is true, so the program says that the next character is a space.

Frequently Asked Questions

It stops the >> operator from skipping whitespace, so spaces and new lines are read as ordinary characters.
Use cin >> std::noskipws >> c, or cin.get(c). Both give you the space as a character.
The leading space is no longer skipped, so >> sees a space where it expects a digit and fails. Turn skipws back on before reading numbers.
get() always reads one raw character. noskipws changes the setting of the stream so >> reads raw characters too.
Use std::skipws, or cin.setf(std::ios::skipws).

Next: nounitbuf

Stop flushing after every output operation.

nounitbuf tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful