[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Re: while (cin >> x

Barry Schwarz

6/11/2016 10:45:00 AM

On Fri, 10 Jun 2016 13:26:56 -0700 (PDT), Andrew Z <formisc@gmail.com>
wrote:

>On Thursday, June 9, 2016 at 1:21:10 PM UTC-4, Barry Schwarz wrote:

<snip>

>> What do you mean when you say cin is empty?
>
>Barry,
> thank you for help.
> This is how i _imagine_ the cin works.

Your imagination is incorrect.

>[code]
>cout<< " Please enter your First name: ";
> string name;
> cin >> name ;
> cout << "Hello, " << name << "!" << endl ;
>[/code]
>So at line 3 (cin >> name), the value read from cin is assigned to "name".
>Now, lets say i want to ask for input twice:
>[code]
>cout<< " Please enter your First name: ";
> string name;
> cin >> name ;
> cout << "Hello, " << name << "!" << endl ;
> string Lname;
>cout<< " Please enter your Last name: ";
> cin >> Lname ;
> cout << "Hello, " << name << "!" << endl ;
>[/code]
>The cin has value(s) for "name" first and then (after some time) values for "Lname".
>(again -in my mind) After i read the input (into a variable) the cin gets cleared out, and the next input value can be read. This is obviously wrong, since a "while" condition, obtains values for everything that was entered previously.

That last sentence makes no sense. For visualization purposes,
imagine that there is a FIFO queue associated with cin. Something
like a paper towel dispenser where a supply of towels is added at the
top and towels are pulled off one by one at the bottom. cin's job is
to transfer data from the queue to a variable. If the variable is a
string, the data is transferred as is. If the variable is an int, the
data is first converted before being stored in the variable. Similarly
for other types of variables.

In most cases, when cin extracts characters from the queue, it will
ignore any white space until it finds a non-white space one. It then
extracts "meaningful" characters until it has a reason to stop. The
two normal reasons to stop are finding another white space character
or finding a character that is invalid for the variable type. Two
abnormal reasons for stopping are an error when data is being
transferred to the queue or a signal indicating there is no more data
(referred to commonly as end of file). Simply having an empty queue
is neither an error nor a terminating condition.. It simply means cin
must wait until more data arrives.

>Here is the complete original code:
>[code]
>int main() {
> cout<< " Please enter your First name: ";

At this point it would be perfectly legal for you to enter
" John 96 82 88 93 79 " and press Enter. As a result, the queue
would contain
" John 96 82 88 93 79 \n"

To see how the processing differs when you enter the data one at a
time, see iteration 4 below.

> string name;
> cin >> name ;

The white space is skipped, John is transferred to name, and the queue
no contains
" 96 82 88 93 79 \n"

> cout << "Hello, " << name << "!" << endl ;
> cout << "Please enter your Midterm and Final exam grades: " ;
> double midterm, final;
> cin >> midterm >> final ;

The white space is skipped, midterm is set to 96.0, white space is
skipped, final is set to 82.0, and the queue now contains
" 88 93 79 \n"

> // ask for homework grades
> cout << " Please enter your Homework grades, "
> " Followed by the end-of-flle: " ;
>
> // the number and sum of the grades read so far
> int count = 0;
> double sum = 0;
> // the variable into which to read
> double x ;
> /*
> invariant:
> we have read count grades for far, and sum is the sum of the first count grades
> */
>
> while (cin >> x) {

In iteration 1, white space is skipped, x is set to 88.0, the
condition evaluates to true, and the queue now contains
" 93 79 \n".

In iteration 2, white space is skipped, x is set to 93.0, the
condition evaluates to true, and the queue now contains
" 79 \n".

In iteration 3, white space is skipped, x is set to 79.0, the
condition evaluates to true, and the queue now contains
" \n".

In iteration 4, white space is skipped and the queue is empty. cin
waits for more typing. The condition is still being evaluated. You
type "91" and press Enter. The queue now contains
"91\n". x is set to 91.0, the condition evaluates to true, and the
queue now contains
"\n".

In iteration 5, the initial processing the same as iteration 4. Now
you signal EOF (^D on Unix, ^Z on Windows, etc) and press Enter. The
while condition evaluates to false and you exit the while loop.

> ++count;
> sum += x ;

These statements are process only during iterations 1-4, not 5.

>}
> // rest of the code is removed.
> return 0;
>}
>[/code]
>
>Thank you.

--
Remove del for email
1 Answer

Andrew Z

6/11/2016 4:02:00 PM

0

On Saturday, June 11, 2016 at 6:44:59 AM UTC-4, Barry Schwarz wrote:

> In iteration 5, the initial processing the same as iteration 4. Now
> you signal EOF (^D on Unix, ^Z on Windows, etc) and press Enter. The
> while condition evaluates to false and you exit the while loop.
>
> > ++count;
> > sum += x ;
>
> These statements are process only during iterations 1-4, not 5.
>

Barry , perfect and detailed explanation. Thank you very much for finding time and putting the effort in breaking this down for me.
Finally i got it.

Thank you
Andrew