1

So I need to store user inputs in a vector until he entersa blank line (or an invalid input). I've tried to convert the input to string, then compare it to a blank line, but it didn't work. Here's the code:

int input;
vector<int> v;
do
{
    cout << "Write a number!" << endl;
    cin >> input;
    v.push_back(input);
} while (to_string(input) != "");

Is there any way to do that?

UPDATE

Thank you guys so much for your help! The second answer solved all of my problems and the first one helped me to understand the logical background of it.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
RangerFox
  • 15
  • 6
  • Use `std::getline` to get a line, check if it is an empty string. If it is not an empty string, use `std::stringstream ss(input);` to `ss >> input;`. – Eljay Oct 01 '19 at 12:25
  • [Read full lines into a string](https://en.cppreference.com/w/cpp/string/basic_string/getline), strip leading and trailing white-space, and see if the line is empty. If it is then you have an empty line, else attempt to convert the string to a number. – Some programmer dude Oct 01 '19 at 12:25
  • 1
    Possible duplicate of [Keep looping until user enters a blank line?](https://stackoverflow.com/questions/18041615/keep-looping-until-user-enters-a-blank-line) – anatolyg Oct 01 '19 at 12:30

2 Answers2

1

You can reverse your logic: instead of reading integers until something, you can read strings, check if they are empty and then convert them to integers.

std::string input;
std::vector<int> v;
std::getline(std::cin, input);
while(!input.empty())
{
    int number = std::stoi(input);
    v.push_back(number);
    std::getline(std::cin, input);
}

Notice that std::cin will not work, because it ignores whitespace (including newline character). Also, mixing std:: cin >> with std::getline is a bad idea

Yksisarvinen
  • 13,037
  • 1
  • 18
  • 42
  • This is troublesome because all input with the stream extract operator `>>` by default skips white-space. And the newline is a white-space character. So `std::cin >> input` will only return if some non-space input is entered. – Some programmer dude Oct 01 '19 at 12:27
  • @Someprogrammerdude Hmm, that's right. I'll change it to getline – Yksisarvinen Oct 01 '19 at 12:28
0

Use standard function std::getline. Here is a demonstrative program

#include <iostream>
#include <string>
#include <vector>

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

    while ( true )
    {
        std::cout << "Enter a number (Enter - exit): ";
        std::string line;

        if ( not std::getline( std::cin, line ) or line.empty() ) break;

        try
        {
            int value = std::stoi( line );
            v.push_back( value );
        }
        catch ( ... )
        {
            std::cout << "Not an integer number\n";
        }           
    }

    for ( const auto &item : v ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

Its output might look like

Enter a number (Enter - exit): 1
Enter a number (Enter - exit): 2
Enter a number (Enter - exit): 3
Enter a number (Enter - exit): A
Not an integer number
Enter a number (Enter - exit): 4
Enter a number (Enter - exit): 5
Enter a number (Enter - exit): 
1 2 3 4 5
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268