Phantom
Phantom

Reputation: 23

Read a sequence of ints from cin and store them in a vector

This is what I have done to read integers with std::cin and store them in a vector:

int number; 
vector<int>ivec;

while (cin>>number)
{       
    ivec.push_back(number);
}

for (auto v: ivec){

    cout << v;
}

Then, I am stuck with the problem that how to stop entering integers and move to the next process of printing the vector out. Any pointer will be appreciated.

Upvotes: 0

Views: 2558

Answers (4)

Pankaj
Pankaj

Reputation: 431

Please find a simple solution to your problem, let me know if you see any issue with this solution.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> va;
    int x;
    while ( cin >> x ) {
          va.push_back(x);
          if ( cin.get() == '\n' ) break;
    }
    //Vector output
    for ( int i = 0; i < va.size(); i++ ) {
        cout << va[i] <<" ";
    }
    return 0;
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

You can do it the following way

#include <vector>
#include <iterator>

//...

vector<int> ivec( std::istream_iterator<int>( std::cin ), 
                  std::istream_iterator<int>() );


for ( auto v: ivec ) std::cout << v << ' ';
std::cout << std::endl;

In Windows you have to press key combination Ctrl + z and in UNIX system Ctrl + d

Or you can introduce a sentinel. In this case the loop can look like

int number; 

const int sentinel = SOME_VALUE;

for ( std::cin >> number && number != sentinel ) vec.push_back( number );

Upvotes: 0

Michael Karcher
Michael Karcher

Reputation: 4041

The code you posted reads numbers from cin as long as it succeeds. There are two ways to have it stop succeeding: If you enter something that is not a number, reading data still succeeds, but converting it to number fails. This puts cin into the bad state. It can be recovered from using the clear methode. The other way is making the reading of characters from cin fail (for example the end of a file that gets used as input). This puts cin into the failed state. Usually, recovering from a failed state is impossible.

To produce the you can no longer read state at end of file when entering data at the keyboard, operating system specific methods have to be used (likely Control-D or Control-Z). This is final for the invocation of your program.

If you need a way for the user to signal: "Please go on, but let me enter other stuff later", the most clean way is likely reading cin line-by-line and parse the input using strtol or a stringstream, and comparing for a magic stop-token (e.g. empty line, "end") to exit the loop.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385204

It depends on the terminal in use and the precise mechanism varies quite a lot but, conventionally, typing Ctrl+D (Linux) or Ctrl+Z (Windows) will result in an end-of-file "signal" being transmitted along the pipe, causing the EOF bit to be set on cin, and thus the next cin >> number attempt to fail.

That will break the loop.

Conveniently, the same will happen if you ran your executable with redirected input from a file. Which is kind of the point.

Upvotes: 1

Related Questions