while (cin >> NumData >0)

As written in the title, this won't work. If I try writing a 0 it DOES goes into the loop.
Can't it be done inside the while?

Yes that doesn't work. You're trying two things at once. What about
1
2
3
4
5
6
7
while (cin >> NumData)
{
  if(NumData >0)
  {
     // do something
  }
}
It did work with reading from files, haha .

Thanks for your help ;)
closed account (S6k9GNh0)
You can do something like "(cin >>myStreamable) >> myStreamDataHolder" though. It's rather hard to read but some people do it.
closed account (D80DSL3A)
Ah, a use for the comma operator!
1
2
3
4
5
6
7
8
9
10
int main(void)
{
	int NumData;
	cout << "Enter NumData: ";
	while( cin >> NumData, NumData > 0 )
		cout << "Enter NumData: ";

	cout << "Bye-bye" << endl;
	return 0;	
}

EDIT: All statements in the parentheses are executed, but only the last is used for the logical test.
You can even put the prompt in there!
1
2
3
4
5
6
7
int main(void)
{
	int NumData;	
	while( cout << "Enter NumData: ", cin >> NumData, NumData > 0 );
	cout << "Bye-bye" << endl;
	return 0;	
}
Last edited on
closed account (S6k9GNh0)
Wow, that's an interesting use indeed. You know, I didn't even know there was a comma operator until I was looking at how boost assign worked yesterday.
That does defeat the point of having the read inside of the while loop though, to check for an error in reading via cin.
closed account (D80DSL3A)
True, input validation can't be done with that method.
I just meant to show how what ramako was attempting to do could be done.

@ramako. If a non-integer value is entered then cin will fail and there will be no way to clear that condition. An infinite loop results. The method is therefore NOT recommended.
Topic archived. No new replies allowed.