Juventino
Juventino

Reputation: 51

Read multiple integers in the same line with cin in C++

I have a problem reading integers from the user. I could use

int a, b, c;
cin >> a >> b >> c;

But I don't know how much integers the user will introduce. I have tried this:

    int n;
    cin >> n; //Number of numbers
    int arrayNumbers[n];

    for(int i=0;i<n;i++){
    cin>>arrayNumbers[i]
    }

And the input of the user will be like: 1 2 3 I mean, in the same line. Using my previous code, it only gets the fist number and not the rest.

How could I do it?

Upvotes: 3

Views: 14473

Answers (2)

Hikmat Farhat
Hikmat Farhat

Reputation: 592

Use std::getline() to read the whole line into a string first. Then create a stringstream from the input string. Finally use a istream_iterator to iterate over the individual tokens. Note that this method will fail at the first input that is not an integer. For example if the use inputs: " 1 2 ab 3" then your vector will contain {1,2}.

int main() {
        std::string str;
        //read whole line into str
        std::getline(std::cin, str);
        std::stringstream ss(str);
        //create a istream_iterator from the stringstream
        // Note the <int> because you want to read them
        //as integers. If you want them as strings use <std::string>
        auto start = std::istream_iterator<int>{ ss };
        //create an empty istream_iterator to denote the end
        auto end= std::istream_iterator<int>{};
        //create a vector from the range: start->end
        std::vector<int> input(start, end);
    
    }

Upvotes: 6

lbragile
lbragile

Reputation: 8122

Rather than using a static array int arrayNumbers[n] which requires you to know how many numbers the user will enter prior to compilation, you should use a dynamic array such as std::vector<int> which can change size during run time.

For example:

#include <iostream> 
#include <vector>

using namespace std;

int main()
{
    vector<int> vecNumbers; // a dynamic array of integers
    while(!cin.fail())
    {
        int value;
        cout << "Enter a value: ";
        cin >> value; // don't forget the semicolon
        if(!cin.fail()) // not having this will add another 0 to the vector
            vecNumbers.push_back(value);
    }
    
    // print the final vector (can ignore for your question)
    vector<int>::const_iterator vecItr;
    cout << "\nvecNumbers = [";
    for(vecItr = vecNumbers.begin(); vecItr != vecNumbers.end(); vecItr++)
    {
        if(vecItr != vecNumbers.end()-1)
            cout << *vecItr << ", ";
        else
           cout << *vecItr << "]";
    }

    return 0;
}

Output:

Enter a value: 5
Enter a value: 6
Enter a value: 7
Enter a value: 8
Enter a value: s

vecNumbers = [5, 6, 7, 8] 

Notice that !cin.fail() detects when the input type doesn't match and thus ends the loop. So you could have a statement that mentions this, like "when done, enter any character to end".

Also note that I #include <vector> and using namespace std, if you don't then you can simply use std::vector<int>, std::cout, and std::cin.

Upvotes: 1

Related Questions