C++ ws Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, ws is an input manipulator. When you write in >> std::ws, the stream reads and throws away all whitespace characters in front: spaces, tabs, and new lines. It stops at the first character that is not whitespace.

The most useful place for it is before getline(), to skip the leftover new line after cin >> number. Include <iostream> to use it.

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::ws into an input stream with >>, or pass the stream to getline() that way.

C++
std::cin >> n >> std::ws;              // skip whitespace after n
std::getline(std::cin >> std::ws, line);   // skip, then read a line
CallWhat it does
in >> std::wsSkips all whitespace in front of the next character.
std::getline(in >> std::ws, line)Skips whitespace first, then reads a line.
in >> n >> std::wsReads n, then removes the whitespace that follows.

ws skips all whitespace, including empty lines. ignore() skips only to the end of one line.

Library declarations
C++
template<class CharT, class Traits>
basic_istream<CharT, Traits>& ws(basic_istream<CharT, Traits>& is);

Parameters

ParameterDescription
isThe input stream. It is passed in for you when you write in >> ws. You never write it yourself.

Return Value

Return ValueDescription
istream&A reference to the same stream. If the end of the input is reached, eofbit is set.

C++ Examples

Example 1 — Skip Spaces Before getline()

The leading spaces are removed, so the line starts with the first real letter.

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

int main() {
    std::istringstream in("   hello world");
    std::string line;

    std::getline(in >> std::ws, line);

    std::cout << "[" << line << "]\n";
    return 0;
}

How It Works

  1. in >> std::ws reads and drops the three spaces and stops before h.
  2. It returns the stream, so it can be passed straight to getline().
  3. getline() reads the rest of the line. The brackets show that the text starts at h.

Example 2 — Fix the >> then getline() Problem

ws removes the new line that >> leaves behind.

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

int main() {
    std::istringstream in("25\nMari Selvan\n");
    int age = 0;
    std::string name;

    in >> age >> std::ws;
    std::getline(in, name);

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

How It Works

  1. in >> age reads 25 and stops before the new line.
  2. >> std::ws drops that new line, so the stream now starts at M.
  3. getline() reads Mari Selvan in full. Without ws it would have returned an empty line.

Example 3 — ws on a Stream with Only Spaces

There is nothing but whitespace, so ws runs into the end of the input.

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

int main() {
    std::istringstream in("   ");

    in >> std::ws;

    std::cout << "eof: " << (in.eof() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. The stream holds three spaces and nothing else.
  2. ws skips all three and then looks for one more character, but the stream has ended.
  3. That sets eofbit, so eof() is true. This is a handy way to ask "is there anything left besides spaces?".

Frequently Asked Questions

It skips whitespace (spaces, tabs, new lines) at the start of an input stream, until it finds a character that is not whitespace.
Use cin >> n >> std::ws; or use cin.ignore(1000, '\n'). Both remove the new line that >> left in the stream.
ws skips all whitespace, even many lines. ignore(n, '\n') skips to the end of the current line, whatever the characters are.
skipws is a setting that makes every >> skip whitespace. ws is a one-time skip that you ask for by hand.
That form skips whitespace and then reads a line. It is a short way to read the next non-blank line.

Back to the function index

See all 60 iostream and iomanip guides grouped by what they do.

Function index →

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