How to take arbitrary number of number input using cin in c++

Viewed 633

I want to take input from the user until he presses enter key. I have used the following approach, but in vain, as the loop won't terminate. Please help.

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n;
    vector<int> array;
    cout<<"Please enter the elements of the array"<<endl;
    while(cin>>n) {
        array.push_back(n);
    }
    for(auto i: array){
       cout<<i<<" ";
    }
    cout<<endl;
    return 0;
}
3 Answers

The while loop will go on until cin >> n fails. It doesn't fail when you press enter. Enter is treated like any other whitespace character and will be skipped by default when doing formatted reading using operator>>.

You could read complete lines and extract data from those instead. If the user enters a blank line, you break out of the loop.

#include <iostream> // include the correct headers, not bits/stdc++.h
#include <sstream>
#include <string>
#include <vector>

// don't do: using namespace std;

int main() {
    std::vector<int> array;

    for(std::string line; std::getline(std::cin, line);) {

        if(line.empty()) break; // the user didn't enter anything, break out

        // put the line in an istringsstream for extraction
        std::istringstream is(line);
        int n;
        while(is >> n) { // extract from the istringstream
            array.push_back(n);
        }

        // if you want to break out of the loop if the user entered
        // something that couldn't be extracted:
        if(not is.eof()) { // if all was extracted, eof() will be true
            is.clear(); // clear the error state from the failed extraction
            std::getline(is, line); // read out the rest of the line
            std::cout << "could not extract an int from \"" << line << "\".\n";
            break;
        }

    }
    
    // print the result
    for(auto i: array) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
}

If you only want exactly one line of data, replace the for loop with

if(std::string line; std::getline(std::cin, line)) {

and adjust the breaks accordingly (since there's no loop to break out of).

You can use std::peek function to check if the next character is a newLine.

while(cin.peek() != '\n'  && cin>>n) {
    array.push_back(n);
}

Alternatively, you can enter an int at the beginning of your input determining the size of your input array.

int arr_size;
cin >> arr_size;
// Take input for that array according to its entered size

Or even take the whole input as string using getline and parse that string to get your array numbers.

As, already mentioned in the comments, there is no end-of-line, during command line inputs, you have to manually press ctrl + z.
But, you can use this alternate approach by using string::empty.

#include <iostream> // Only include things you use
#include <string>
#include <vector>

// Using namespace std

int main() {
    std::vector<int> array;
    std::string s;

    while(getline(std::cin, s)) {

        if(s.empty())   // user give empty string as input.
            break;

        array.push_back(stoi(s));   // using stoi (one of many string methods)

        s.clear();  // Clear the string for the next input.
    }
    
    // print the result
    for(auto i: array) {
        std::cout << i << " ";
    }

    std::cout << std::endl;
}
Related