mercredi 25 mai 2016

Unknown size vector input

here is the question. I have a vector of ints, and user should enter it's elements as long as he or she want. Then, this vector is passed to a function, which calculates it's size for whatever reason. Is there any way to do so? I found some code on shores of the Internet:

std::vector<int> vec;

std::string buffer = "";

std::cout << "Enter numbers. "
          << "After you finish, enter newline" << std::endl;

do {
    std::getline(std::cin, buffer);
    if (buffer.size() > 0) {
        input.push_back(atoi(buffer.c_str())); //stoi was not declared =(
    }
} while (buffer != "");

But this code doesn't works in right way: I choose entering numbers and it just does nothing. Have no idea what's wrong with it. (c++11 enabled)

Aucun commentaire:

Enregistrer un commentaire