In C++, skipws makes the input operator >> skip whitespace (spaces, tabs, and new lines) before it reads a value. This is why you can type 12 7 with extra spaces or on two lines and still get two numbers.
It is on by default. You use skipws to switch the behaviour back on after noskipws. 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::skipws into an input stream.
C++
std::cin >> std::skipws >> a >> b; // spaces are skipped
std::cin >> std::noskipws >> c; // next character, even a space
Call
What it does
std::cin >> std::skipws
Whitespace in front of each value is skipped.
in >> std::skipws >> n
Reads n even if spaces come first.
in >> std::noskipws
The opposite: whitespace is read like any other character.
Because it is the default, you almost never write skipws. It matters when you had turned it off with noskipws.
Library declarations
C++
ios_base& skipws(ios_base& str);
Parameters
Parameter
Description
str
The stream. It is passed in for you when you write cin >> skipws. 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 skipws.
C++ Examples
Example 1 — Read a Number After Spaces
The spaces in front are skipped because skipws is on.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream in(" 42");
int n = 0;
in >> n;
std::cout << "n = " << n << '\n';
return 0;
}
Output
n = 42
How It Works
The text has three spaces before the number.
>> skips the spaces first, since skipws is the default setting.
Then it reads the digits, so n becomes 42.
Example 2 — Read Letters Separated by Spaces
Each >> jumps over the spaces and takes the next visible letter.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("a b c");
char x = 0;
char y = 0;
char z = 0;
in >> std::skipws >> x >> y >> z;
std::cout << x << y << z << '\n';
return 0;
}
Output
abc
How It Works
The text a b c has spaces between the letters.
With skipws each >> ignores the spaces in front of the next letter.
x, y, and z hold a, b, and c, and they print without any spaces.
Example 3 — Turn skipws Back On After noskipws
Read a space on purpose, then go back to skipping.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("x y");
char a = 0;
char b = 0;
char c = 0;
in >> std::noskipws >> a >> b;
in >> std::skipws >> c;
std::cout << "a=" << a << " b=" << (b == ' ' ? "space" : "other") << " c=" << c << '\n';
return 0;
}
Output
a=x b=space c=y
How It Works
noskipws stops the skipping, so a gets x and b gets the space character.
skipws turns skipping back on. No whitespace is left in front of y, so it makes no difference here, but it matters for later reads.
c reads y. The output shows that b really held a space.
Frequently Asked Questions
It makes >> skip whitespace characters (spaces, tabs, new lines) before it reads a value.
Yes. That is why cin >> a >> b works even when you type extra spaces between the numbers.
No. Only the formatted input operator >> skips whitespace. get() and getline() read every character.
Use cin >> std::noskipws >> c, or use cin.get(c). Then the space is returned like any other character.
skipws skips leading whitespace on input. noskipws does not, so whitespace is read as normal characters.