How to Take std::cin Input with Spaces? Last Updated : 01 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, std::cin is used to accept the input from the standard input device. By default, the delimiter used by std::cin to separate input is a whitespace. In this article, we will learn how to take std::cin input with spaces in C++. Example: Input: Hey! Geek Welcome to GfG //input with spacesOutput: Input Entered: Hey! Geek Welcome to GfGstd::cin Input with Spaces in C++To take std::cin input with spaces we need to customize the behavior of the standard input stream by treating the newline character ('\n') as a delimiter in place of a space. To achieve this use the below approach: Approach: Define a custom locale facet that modifies the character classification table used by the locale to alter how characters are interpreted by the input stream.Inside the custom locale change the classification of the newline character ('\n') to be considered as a space character. Finally, apply the custom locale by using cin.imbue() to set the locale of the std::cin with a new locale.C++ Program to Take std::cin Input with SpacesThe below program demonstrates how we can take std::cin input with spaces in C++. C++ // C++ program to take std::cin input with spaces #include <iostream> #include <locale> // to work with locales #include <string> using namespace std; // Defining a structure that inherits from ctype<char> struct changeDelimiter : ctype<char> { // Constructor for changeDelimiter changeDelimiter() : ctype<char>( createTable()) // Initializing the base class // ctype<char> with a table { } // Static function to create a table with custom // settings static mask const* createTable() { static mask rc[table_size]; // Creating a table with the // size of the character set rc['\n'] = ctype_base::space; // Set the newline // character to be treated // as whitespace return rc; // Return the modified table } }; int main() { // Creating a custom locale with the changeDelimiter // facet cin.imbue(locale(cin.getloc(), new changeDelimiter)); // prompt the user to enter the input with spaces cout << "Enter Input with Spaces: " << endl; string inputString; // Read input from cin, which has the modified locale cin >> inputString; // Print the input string cout << "Input Entered: " << inputString << endl; } Output Enter Input with Spaces: Hey! Geek Welcome to GfG Input Entered: Hey! Geek Welcome to GfG Comment More infoAdvertise with us Next Article How to Handle Multiple String Inputs with Spaces in C++? A anujpato5vy Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-advanced CPP Examples +1 More Practice Tags : CPP Similar Reads How to Take Long String Input With Spaces in C++? In C++, we often take long strings with a lot of characters as inputs with spaces but if we use cin then our input gets terminated as soon as whitespace is encountered. In this article, we will discuss how to take long strings with spaces as input in C++. Take Long String Input With Spaces in C++To 2 min read How to Use stringstream for Input With Spaces in C++? In C++, the std::stringstream class is a stream class to operate on strings and is very useful when we want to operate on a string as if it were a stream (like cin or cout). In this article, we will learn how to use string streams for input with spaces in C++.Example:Input: string = âHello, World!âO 2 min read How to Handle Multiple String Inputs with Spaces in C++? In C++, strings are a sequence of characters that might contain spaces in many cases but we can read only the input text till whitespace using cin. In this article, we will learn how to handle multiple string inputs with spaces in C++. Example: Input:Enter multiple strings:String1String2Output:You E 2 min read How to Take Operator as Input in C++? Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as 2 min read How to Read a Paragraph of Text with Spaces in C++? In C++, we may need to read the input that includes the paragraph of text with spaces. In this article, we will learn how to read a paragraph of text with spaces in C++. Reading a Paragraph in C++To read a paragraph of text that includes spaces, we can use the std::getline() function that can read t 2 min read How to Take Multiple Line String Input in C++? In C++, taking string input is a common practice but the cin is only able to read the input text till whitespace. In this article, we will discuss how to read the multiple line of text input in C++. For Example, Input:Enter Your Text: This is amultiline text.Output:You Have Entered:This is amultilin 2 min read Like