Reading an unknown number of inputs and adding them in a Vector

Consider the following piece of Code:

1
2
3
4
5
6
7
8
9
10
11
int ReadNumbers()
{
    int num;
    vector<int> x;
    cout << "Enter Numbers" << '\n';
    while(cin >> num)
    {
        x.push_back(num);
        cout << num << " was added to the Vector" << '\n';
    }
}


The while loop is expected to terminate when the user provides an Invalid Input. But this while loop behaves unexpectedly when the user provides a 'Newline' input (by pressing Enter) and becomes an infinite loop. How can I prevent this from happening? Also I've heard that expecting invalid inputs isn't good code design. Is this true? If yes, then how can I solve my question without expecting Invalid Inputs?
Why did you decide that the loop behaves unexpectedly when the user enter a new;ine? The new line is a white space character that will be skipped. It is expected behavior of this loop.
Because we are entering an unknown number of inputs, the loop has to terminate at some point when the user is finished with his inputs. That is what I want. But entering a newline in this case causes the while loop to behave unexpectedly.
But just pressing enter will not cause the stream to enter an error state. To exit this loop you will need to enter some invalid value or perhaps by signaling eof() by using the control D or control Z key combination.
Last edited on
I'm using NetBeans and have tried pressing Ctrl + Z and that doesn't work. And what is eof()? How does it help to solve this question?
You should press Ctrl+ z after pressing Enter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <vector>
#include <iostream>

std::vector<int> ReadNumbers()
{
    std::vector<int> numbers ;

    do
    {
        int input ;
        if ( std::cin >> input )
            numbers.push_back(input) ;
    } while ( std::cin && std::cin.peek() != '\n' ) ;

    return numbers ;
}

void print(const std::vector<int> & v)
{
    std::cout << "{ " ;
    for ( auto element : v )
        std::cout << element << ' ' ;
    std::cout << "}\n" ;
}

int main()
{
    print(ReadNumbers()) ;
}


It doesn't terminate the input if the initial input(s) are newlines, but it should be easy enough to modify if you want that behavior.
Last edited on
I'm using NetBeans and have tried pressing Ctrl + Z and that doesn't work.

What operating system are you using? Ctrl + Z should be eof() for a Windows operating system and Ctrl-D should be eof() for Linux.

And what is eof()?
End of file. Remember cin is a file stream.

How does it help to solve this question?

To exit your loop you must somehow put cin into an error state. And eof() is an error state.
Last edited on
@cire:

1
2
3
4
5
6
do
    {
        int input ;
        if ( std::cin >> input )
            numbers.push_back(input) ;
    } while ( std::cin && std::cin.peek() != '\n' ) ;


Please explain how this code works. Specifically, i didn't understand what cin && cin.peek() do in the while loop
When you use cin where a boolean value is expected, it returns the error state of the stream. If it's not in an error state it evaluates to true, and if it is in an error state it evaluates to false.

You're doing essentially the same thing with while ( cin >> num ).

cin.peek() returns the next character in the input stream without extracting it.

So while (std::cin && std::cin.peek() != '\n') could be restated in English as: while cin is not in an error state and the next character in the stream is not a newline.
Ok Thanks! :) By the way, can anyone suggest an introductory book for learning C++? I'm already using C++ Primer but I didnt like it (I've read the first 4 chapters )
Last edited on
Topic archived. No new replies allowed.