4

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?

Aimery
  • 1,559
  • 1
  • 19
  • 24
Hnim Mahp
  • 57
  • 3

4 Answers4

6

The int is converted to a bool implicitly. See here:

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.

Blaze
  • 16,736
  • 2
  • 25
  • 44
3

It may work as a boolean when we input something else for myint but I think 0 is still qualified as an integer.

I think this is where your misunderstanding lays. We can't "input something else for myint". It can only ever hold an integer value. If the input stream can't parse an integer, it won't be putting anything into myint, it has nothing to put.

So you can't be checking that myint holds an integer or something else, there is nothing else to hold except integers. As such, the only question is how an integer can be tested for truthiness. As you have been told already, integers are convertible to bools. 0 is false, and anything else is true. That is what the loop condition checks. What it can only ever check at all.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
3

Others have already explained why while(myint) stops when myint is 0, so instead I want to give you an idea of what you can do instead.

First you can look up the return values on operator>>:

The extracted value or sequence is not returned, but directly stored in the variable passed as argument. Errors are signaled by modifying the internal state flags

That means that you can use the call std::cin.good() to check if anything has gone wrong with your reads. Then the loop stops when you give it an input that isn't an int (or whitespace, operator>> ignores that by default).

do {
    std::cin >> myint;
    if (std::cin.good())  // Keep the last, failed read from inserting a 0
        myvector.push_back(myint);
} while (std::cin.good());

Hopefully this gives you some ideas you can build on.

Frodyne
  • 3,547
  • 6
  • 16
2

From the C++ 17 Standard (7.14 Boolean conversions)

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct- initialization (11.6), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

And (9 Statements)

  1. ...The value of a condition that is an expression is the value of the expression, contextually converted to bool for statements other than switch;...

Thus the condition in the do-while statement is equivalent to

  do {
    std::cin >> myint;
    myvector.push_back (myint);
  } while ( myint != 0 );

If you need to exclude the value 0 from pushing it into the vector then the loop should be rewritten the following way

while ( ( std::cin >> myint ) and ( myint != 0 ) )
{
    myvector.push_back( myint );
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335