?
????
this is an excerpt from C++ Primer 3rd edition by Lippman, Lajoie.
How might we read an unknown number of input values? At the end of
Section 1.2, we did just that. The
code sequence
string word;
while ( cin >> word )
// ...
reads one string from standard input with each iteration of the while
loop until all the strings are read. The
condition
( cin >> word )
evaluates to false when the end-of-file is reached (how this occurs is
explained in Chapter 20). Here is a
simple program that uses the code sequence:
#include <iostream>
#include <string>
int main()
{
string word;
while ( cin >> word )
cout << "word read is: " << word << '\n';
cout << "ok: no more words to read: bye!\n";
return 0;
}
The following are the first five words of James Joyce's novel
Finnegans Wake:
riverrun, past Eve and Adam's
When these words are entered at the keyboard, the output of the
program is as follows:
word read is: riverrun,
word read is: past
word read is: Eve
word read is: and
word read is: Adam's
word read is: ok: no more words to read: bye!
(In Chapter 6 we'll look at how we can remove the punctuation from the
various input strings.)
setting aside how the condition evalutes to false.
isn't it strange??
I wonder how there can be half-true condition. I think there should
only be either true or false.
the result above are very unreasonable. the part
word read is: ok: no more words to read: bye!
the result above is from partly executed statement.
if the condition ( cin >> word ) evaluates to false, there's no
execution of cout << "word read is: " << word << '\n'; at all.
I think this condition should, if possible, evaluate to false right
after Adam's is tested. I wonder if I'm right.
I would like to hear from someone who knows this language well.
How might we read an unknown number of input values? At the end of
Section 1.2, we did just that. The
code sequence
string word;
while ( cin >> word )
// ...
reads one string from standard input with each iteration of the while
loop until all the strings are read. The
condition
( cin >> word )
evaluates to false when the end-of-file is reached (how this occurs is
explained in Chapter 20). Here is a
simple program that uses the code sequence:
#include <iostream>
#include <string>
int main()
{
string word;
while ( cin >> word )
cout << "word read is: " << word << '\n';
cout << "ok: no more words to read: bye!\n";
return 0;
}
The following are the first five words of James Joyce's novel
Finnegans Wake:
riverrun, past Eve and Adam's
When these words are entered at the keyboard, the output of the
program is as follows:
word read is: riverrun,
word read is: past
word read is: Eve
word read is: and
word read is: Adam's
word read is: ok: no more words to read: bye!
(In Chapter 6 we'll look at how we can remove the punctuation from the
various input strings.)
setting aside how the condition evalutes to false.
isn't it strange??
I wonder how there can be half-true condition. I think there should
only be either true or false.
the result above are very unreasonable. the part
word read is: ok: no more words to read: bye!
the result above is from partly executed statement.
if the condition ( cin >> word ) evaluates to false, there's no
execution of cout << "word read is: " << word << '\n'; at all.
I think this condition should, if possible, evaluate to false right
after Adam's is tested. I wonder if I'm right.
I would like to hear from someone who knows this language well.