4

For example while(getline( , ))

when is this kind of condition true/false and why are they used instead of other situational conditions?

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
  • 1
    Did you look at documentation for the `getline` function? – aschepler Jul 02 '18 at 00:51
  • 2
    This condition is true when `getline()` returns a true value. – melpomene Jul 02 '18 at 00:54
  • Welcome to Stack Overflow. Your questions will be better received if they pertain to a topic that isn't something you can simply look up in any reference manual. Please read the [**About**](http://stackoverflow.com/tour) page soon and also visit the links describing [**How to Ask a Question**](http://stackoverflow.com/questions/how-to-ask) and [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). Providing the necessary details, including your code, and associated errors, if any, will allow everyone here to help you with your question. – David C. Rankin Jul 02 '18 at 00:56
  • 2
    If the condition evaluates to `true` or `false` depends on the streams state that is returned by the [`std::getline()`](https://en.cppreference.com/w/cpp/string/basic_string/getline) function. Basically this is determined by the overloaded [operator for casting to bool](https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool) @melpomene. – πάντα ῥεῖ Jul 02 '18 at 00:57
  • 3
    @David How does that question require a [MCVE]? – πάντα ῥεῖ Jul 02 '18 at 01:29
  • 2
    Also the close vote is completely unjustifiable here. The question is neither _unclear_, nor _too broad_. Looking at the `std::getline()` documentation doesn't explain that behavior obviously. – πάντα ῥεῖ Jul 02 '18 at 01:43
  • @πάνταῥεῖ I think it's unclear. *instead of other* - like what? – klutt Jul 02 '18 at 03:01
  • That was the point -- if the question doesn't require a MCVE -- it's not really a question... – David C. Rankin Jul 02 '18 at 03:36
  • 3
    @David _"if the question doesn't require a MCVE -- it's not really a question."_ Sorry, I strongly disagree with you about that. – πάντα ῥεῖ Jul 03 '18 at 18:25
  • 3
    @DavidC.Rankin Questions (even ones about actual code) don't always require an MCVE. – TylerH Jul 03 '18 at 18:28
  • 2
    [Only debugging questions require code.](https://meta.stackoverflow.com/a/338846/2891664) Also [*Is there really a universal code requirement?*](https://meta.stackoverflow.com/q/291399/2891664) and [*Do we need a close reason for zero-effort questions?*](https://meta.stackoverflow.com/q/260828/2891664) – Radiodef Jul 03 '18 at 18:48
  • 1
    I had no idea that you could or were supposed to have `while (std::getline(etc.))` specifically _because_ I had relied on so heavily on documentation. I only found out about that pretty recently, myself. – hegel5000 Jul 03 '18 at 19:27
  • @hegel5000 Which confirms that this question is useful for future research. Thank you for the feedback! – πάντα ῥεῖ Jul 03 '18 at 19:30

1 Answers1

4

when is this kind of condition true/false

Given the documentation of std::getline() which says the return value is the std::istream reference of the stream that is involved in the operation, it isn't obvious how that stream evaluates to true or false.

This is done using the overloaded cast operator to bool inherited from the std::ios class.
This class defines the state flags indicating the current stream state, and true will be only evaluated if the stream state is good, other states like eof or fail will evaluate to false.


and why are they used instead of other situational conditions?

Highly related Q&A: Why is iostream::eof inside a loop condition considered wrong?

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175