How to take std::cin input with spaces in C++?

How to take std::cin input with spaces in C++?

The std::cin is a standard input stream that reads input data from the user and uses whitespace as a delimiter by default.

In C++, the std::cin is an important feature of the iostream library that gives the program the ability to accept input from the user where the whitespace is used as a delimiter. In this article, we will discuss the behavior, uses, limitations, and best practices with the code using std::cin. 

Table of Contents:

Default Behavior of std::cin in C++

In C++, the std::cin takes the input data from the users and reads continuously until it finds a whitespace character like a space, tab, or a new line, by default.

Example:

Cpp

Output:

The user’s input is read by the std::cin. It stops reading when a space appears in the user’s name when they write it. Thus, it scans the first word and outputs it.

Limitations of std::cin with spaces in C++

Below are the limitations of the std::cin when dealing with the spaces in C++:

1. Word-by-word Input

If a user inputs a full name or sentence that contains spaces, std::cin only reads up to the first space in the input data. Thus, only the first word is read by the std::cin.

2. Loss of Additional Input

As std::cin reads only up to the first space, the additional data is ignored, and only the data before the first space is printed as an output, causing an error if the user wants to print the whole data.

3. Handling of Special Characters

When the user inputs the input data with punctuation marks like commas, the std::cin does not read the whole input. It only reads up to the first space and prints it as an output.

4. No Line Termination

In C++, std::cin does not handle the end of the line automatically. Therefore, if a user wants to give the input in multiple parts, it will only read the first part.

5. No Control over Input Length

The std::cin cannot handle the input length. It means that it does not provide a fixed limit over the input data length that can be read, causing the buffer overflow issue.

Alternative Methods to Overcome the Limitations of std::cin in C++

Below are a few alternative methods to overcome the limitations of std::cin in C++:

1. Using std::getline() for Input with Spaces in C++

The std::cin only reads up to the first space, and it can be fixed with the help of std::getline() because it helps in reading the full sentence rather than just reading to the first space.

Example:

Cpp

Output:

In this code, std::getline() reads the whole input data from the user rather than stopping at the first space, printing the desired output that is required by the user. 

2. Using the Combination of std::cin and std::getline() for Multiple Inputs in C++

The limitations of std::cin can also be overcome by using the combination of std::cin and std::getline(), which can be achieved by placing std::getline() just after std::cin in the codes. This helps by reading the remaining data from the input that the std::cin alone was incapable of reading, which hence will provide a valid output to the users.

Example:

Cpp

Output:

In this code, the input data is read by using both std::cin and std::getline() to provide the full and correct output.

3. Using std::istringstream for Parsing Input in C++

The std::istringstream() is used to parse the input string as a stream that can read the input much more efficiently than the std::cin.

Example:

Cpp

Output:

In this code, std::istringstream is used to read the whole input data and the code, and then the valid output is printed. 

4. Reading the Inputs in Loop in C++

Reading the inputs in a loop helps us to overcome the limitations of C++ by reducing the duplication and repetition of codes. The std::cin is used in a loop to read the inputs in a continuous manner.

Example:

Cpp

Output:

In this code, std::cin and a while loop are used to read the inputs easily, and then the output is printed to the console.

Best Practices of using the std::cin in C++

  • Always verify the input given by the user as valid or invalid.
  • Use the std::getline() to fix the limit of the length of the input.
  • If the input operation fails, clear the input console and re-run the program with the valid input.
  • Always use the same data type to provide the input so that no data is lost.
  • Always keep in mind that the std::cin does not read after the space, and use std::getline when it is necessary to read the full input.
  • When dealing with the strings, use std::istringstream to easily manage the memory issues.
  • Provide the inputs in separate functions to improve the readability and efficiency of the code.

Advanced Handling of the std::cin in C++

While the std::cin is useful for reading the input given by the user, handling its behavior is important to avoid errors. Below are a few advanced methods that can be used to handle the std::cin easily.

1. Using std::cin.ignore() to Handle Buffer Issues

In C++, while using both std::cin and std::getline() in the same program, a frequent problem arises where std::getline() is skipped. It occurs because std::cin leaves a newline character (\n) in the input buffer after reading values such as integers and floating-point numbers. This problem can be removed by using the std::cin.ignore() in place of std::cin.

Example:

Cpp

Output:

In this code, std::cin.ignore is used so that std::getline() would not skipped as it reads the remained data from std::cin.

2.  Using std::cin.clear() to Clear Buffer 

In C++, std::cin.clear() is used in the code to clear the input console when the std::cin enters into the fail state and does not read any input data further, due to the invalid input data given by the user. It helps to overcome the arising buffer issue.

Example:

Cpp

Output:

In this code, std::cin.clear() is used to reset the input console, and std::cin.ignore() removes the incorrect input, therefore allowing the user to enter valid data only.

3. Using std::ws to Ignore Leading Whitespaces

In C++, std::ws is used with the std::cin to remove the leading whitespaces before reading the input data by st::cin, thus preventing throwing errors or reading only input before the space.

Example:

Cpp

Output:

In this code, std::ws is used so that the leading spaces are ignored and std::cin can read the whole input.

Conclusion

In C++, std::cin is an important feature for reading the input data from the user. It does not read the input after a whitespace comes, it means that std::cin only reads a single word. Understanding the limits, best practices, and default behavior provides us with an efficient codewriting practice. Also, we can create more user-friendly applications very easily by learning the workings of std::cin.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner