How to Utilize the setw Manipulator in C++

Jinku Hu Feb 02, 2024
How to Utilize the setw Manipulator in C++

This post shows how to utilize the setw stream manipulator in C++.

Use the std::setw Function to Modify the Width of the Next I/O Operation in C++

Stream manipulators are special objects that modify the input/output formatting and sometimes generate some action. The std::endl function, arguably the most common object utilized in output streams, is indeed a stream manipulator. It prints a newline character and flushes the output buffer.

On the other hand, the std::setw command does not generate any output. Instead, it sets the width of the next I/O to the passed integral argument. Some stream manipulators use arguments; these have to include a <iomanip> header. The following example demonstrates the basic output formatting scenario for the setw manipulator. The setfill manipulator is the natural companion for setw. The former is useful for modifying the fill character with the given char argument.

#include <iomanip>
#include <iostream>
#include <sstream>

using std::cout;
using std::endl;
using std::setw;
using std::string;

int main() {
  cout << "setw() : |" << 11 << "|" << endl
       << "setw(4): |" << setw(4) << 11 << "|" << endl
       << "setw(4): |" << 22 << setw(4) << std::setfill('-') << 22 << 33 << "|"
       << endl;

  return EXIT_SUCCESS;
}

Output:

setw() : |11|
setw(4): |  11|
setw(4): |22--2233|

Alternatively, we can use the setw manipulator to restrict the number of characters extracted from the stringstream object. This method might be useful for char buffers that are declared as fixed arrays. Generally, manipulators are implemented using the function overloading.

Each manipulator is the function that returns a reference to the stream. The pointers to one of these functions are passed to the stream extraction/insertion operators, which are overloaded operators on their own. The argument functions are invoked from the overloaded operators’ bodies.

#include <iomanip>
#include <iostream>
#include <sstream>

using std::cout;
using std::endl;
using std::setw;
using std::string;

int main() {
  std::stringstream ss1("hello there");
  char arr[10];

  ss1 >> setw(6) >> arr;
  cout << arr;

  return EXIT_SUCCESS;
}

Output:

hello

One useful feature of the setw manipulator is to easily set the limit on the user input that needs to be stored in a fixed-sized buffer. We just need to pass the sizeof(buffer) as the argument to setw. Notice that the setw call affects a single I/O operation, and then the default width is restored automatically. Conversely, the setfill modification remains active until the explicit change.

#include <iomanip>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int main() {
  char buffer[24];
  cout << "username: ";
  cin >> setw(sizeof(buffer)) >> buffer;

  return EXIT_SUCCESS;
}

Output:

username:
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook