Back to Writing
NOTESc-cppprogramminginput-handlingstandard-library

C/C++ - Limiting Input Length with cin and setw

May 16, 2022Updated Feb 17, 2026
#include <iomanip>

std::cin >> std::setw(10) >> name;

The setw function sets the maximum number of characters that can be read from input.

The example above limits input to 10 characters.

Note: The width setting only applies to the immediately following input operation.