Re: Chapter 6, how does while(cin) work?

51 views
Skip to first unread message
Message has been deleted

Art Werschulz

unread,
Nov 9, 2018, 7:24:44 AM11/9/18
to ppp-p...@googlegroups.com
Hi.

> On Nov 9, 2018, at 3:36 AM, Ihor Leshchyshyn <i.a.les...@gmail.com> wrote:
>
> The question is: "How does while(win) actually work?". I am trying to figure out how it works for few days and still no results.

I assume you mean
while(cin)
Anyway, an istream (such as cin) can be used in a boolean context. Roughly speaking, its value is false if the previous read failed. For more details, Google "c++ istream boolean" or suchlike.

Art Werschulz
a...@comcast.net



oleg sapovsky

unread,
Nov 9, 2018, 8:27:06 AM11/9/18
to ppp-p...@googlegroups.com
while(cin>>val)

пятница, 9 ноября 2018 г. пользователь Ihor Leshchyshyn написал:
Hello guys,
The question is: "How does while(win) actually work?". I am trying to figure out how it works for few days and still no results.

try

{   double val=0;

    cout<<"Welcome!\n";

    while (cin) {

        Token t = ts.get();

        

        if (t.kind == 'q') break; // 'q' for quit

        if (t.kind == ';')        // ';' for "print now"

            cout << "=" << val << '\n';

        else

            ts.putback(t);

        val = expression();

    }

How can ts.get() look ahead? and how does it evaluate after each loop?

For example, "2+5" ? I thought firstly it reads "2", ts.get(), check 2 "if" conditions, putback and call expression().

So, expression should return 2!(1 loop execution) but it somehow can read next 2 characters.

I don't understand how cin or while(cin) works. Any suggestions will be appreciated!

Thanks in advance!

--
You received this message because you are subscribed to the Google Groups "PPP-public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ppp-public+unsubscribe@googlegroups.com.
To post to this group, send email to ppp-p...@googlegroups.com.
Visit this group at https://groups.google.com/group/ppp-public.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages