I'm trying to find an input in C++ equivalent to Java's in.hasNextInt and I found this.
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
int myint;
std::cout << "Please enter some integers (enter 0 to end):\n";
do {
std::cin >> myint;
myvector.push_back (myint);
} while (myint);
std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
return 0;
}
But I don't really get why the input loop would stop when the input is 0. It has the while(myint)
loop which also confuses me since myint
is an integer and not a boolean. It may work as a boolean when we input something else for myint
but I think 0 is still qualified as an integer. Can anyone please explain this to me?