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'
}
Call
What it does
in >> std::noskipws
Whitespace is no longer skipped.
in >> std::noskipws >> c
Reads the very next character into c.
in >> std::skipws
Turns 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
Parameter
Description
str
The stream. It is passed in for you when you write cin >> noskipws. You never write this argument yourself.
Return Value
Return Value
Description
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;
}
Output
spaces: 3
How It Works
noskipws makes in >> c return every character, spaces included.
The text has one space after a and two after b, so the counter reaches 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;
}
Output
hi there
How It Works
Each in >> c reads one character. With noskipws the space in the middle is a normal character.
out << c writes the character to the second stream, one by one.
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;
}
Output
n = 7
next char is a space
How It Works
in >> n reads 7 and stops at the space. The space stays in the stream.
With noskipws the next >> does not skip that space, so c receives it.
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.