How To Take Space Separated Input In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / How To Take Space Separated Input In C++

How To Take Space Separated Input In C++

BLUF: Mastering How To Take Space Separated Input In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Take Space Separated Input In C++

C++ is renowned for its efficiency. Learn how How To Take Space Separated Input In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the process of acquiring input separated by spaces in C++. When we require space-separated input in C++, we utilize the cin object in conjunction with the extraction operator ">>".

Program 1:

Let's consider a scenario to demonstrate how to input space-separated values in C++:

Example

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
    cout << "Enter multiple numbers separated by spaces: ";
    string inputLine;
    getline(cin, inputLine);
    istringstream iss(inputLine);
    int number;
    vector<int> numbers;
    while (iss >> number) {
        numbers.push_back(number);
    }
    cout<<"Entered number are "<< endl;
    for (int num : numbers) {
        cout << num << endl;
    }	
    return 0;
}

Output:

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's consider another instance to demonstrate how to handle space-separated input in C++:

Example

#include <iostream>
#include <vector>
using namespace std;
int main() {
    cout << "Enter space-separated integers "<< endl;
    cout<< " press (Ctrl+D to end input in Linux/Unix, Ctrl+Z in Windows) to exit "<< endl;
    vector<int> numbers;
   int num;
    while (cin >> num) {
        numbers.push_back(num);
    }
    cout << "Input numbers: ";
    for (int n : numbers) {
        cout << n << endl;
    }
    cout << endl;
    return 0;
}

Output:

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 summary, these two C++ scripts offer straightforward and intuitive approaches for receiving input separated by spaces. The first program employs the istringstream to interpret the input line, whereas the second program directly interacts with the standard cin object. Both scripts showcase the adaptability and ease of C++ in managing user input. They lead users through the input procedure, effectively preserving and presenting the provided information. Whether through parsing strings or accepting input directly, these scripts exemplify efficient strategies for managing space-separated input, serving as beneficial resources for C++ programmers.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience