How to take space-separated input in C++

In this article, we will discuss how to take space-separated input in C++. If we want to take space-separated input in C++, we will use the cin object along with the extraction operator ">>".

Program 1:

Let us take an example to illustrate how to take the space-separated input in C++:

Output:

How to take space-separated input in C++

Explanation:

  • At the beginning of the program, we include some libraries that provide tools for input/output and data manipulation.
  • After that, we start by showing a friendly message asking the user to enter multiple numbers separated by spaces.
  • We create a variable called inputLine to store what the user types. We use getline(cin, inputLine) to read a whole line of input (including spaces) from the user.
  • We use istringstream (input string stream) to break down the inputLine into individual numbers. We create an integer variable called a number to store each number temporarily.
  • As we extract numbers from the istringstream, we store them in a special container called a "vector". Think of a vector as a list where we can put numbers. This way, we keep track of all the numbers the user entered.
  • After reading all the numbers, we show another message saying, "Entered numbers are". After that, we go through the vector and show each number individually.

Program 2:

Let us take another example to illustrate how to take the space-separated input in C++:

Output:

How to take space-separated input in C++

Explanation:

  • In this example, we includes the standard library header files: <iostream> and <vector>. The <iostream> is necessary for input and output operations, while the <vector> creates a dynamic array for storing the input integers.
  • Using namespace std is a directive that tells the compiler to use the std namespace by default.
  • Displaying a message to the user. It informs the user that they should enter space-separated integers and provides instructions on signaling the end of input, which varies depending on the operating system. In Linux/Unix, the user can press Ctrl+D, while in Windows, they can use Ctrl+Z.
  • Declare a vector<int> named numbers. A vector in CPP is a dynamic array that can grow or shrink in size, making it suitable for storing a list of integers. This vector will hold the integers entered by the user.
  • Declare an integer variable num. This variable will temporarily store each integer the user enters before adding it to the numbers vector.
  • The loop will repeatedly read and process the user's input until the user signals that they want to exit.
  • Within the loop, the program uses cin >> num to read an integer from the user. The >> operator is the extraction operator, and it extracts (reads) the next integer value from the input stream (cin).
  • Once an integer is read, the program adds it to the numbers vector using the push_back() method. This appends the integer to the end of the vector, effectively storing all the integers entered by the user in the order they were entered.
  • After the user finishes entering input (typically by pressing Ctrl+D or Ctrl+Z), the loop exits, and the program displays the input.
  • The program outputs a message, "Input numbers: ", followed by the integers stored in the numbers vector. It uses a for loop to iterate through the vector and display each integer separated by a space. The endl statement moves to the next line after displaying the input.

Conclusion:

In conclusion, these two C++ programs provide clear and user-friendly methods for taking space-separated input. Program 1 utilizes the istringstream to parse the input line, while Program 2 uses the standard cin object directly. Both programs demonstrate the flexibility and simplicity of C++ for handling user input. They guide users through the input process and efficiently store and display the entered data. Whether through string parsing or direct input, these programs illustrate effective techniques for handling space-separated input, making them valuable tools for C++ developers.