code contains problem

?

????

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.
 
R

René Kjellerup

???? said:
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.

Well the last string you enter is 0bytes long, and the next time 'cin >>
word' is reached
it will be evaluated as false and continue the program execution.
 
M

Martijn Lievaart

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;
}

This should not compile, you ar missing a "using namespace std;".
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.

This is very strange. I suspect a compiler bug, or this is not the code
you really executed. When I add a "using namespace std;" to the code, it
compiles and runs fine.

HTH,
M4
 
N

Nick Hounsome

???? said:
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!

What EXACTLY did you enter at the keyboard?
Does it work from a file?
 
M

MPBroida

???? said:
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.


Just a typo in the book. The final line of the example
line should/will not have the "word read is: " part.

Mike
 
V

Victor Bazarov

MPBroida said:
Just a typo in the book. The final line of the example
line should/will not have the "word read is: " part.

It depends on what you used to end the phrase when typing it on
the keyboard. If you pressed 'Enter', the stream will contain the
newline character, which will terminate current word ("Adam's") and
will NOT force reading beyond the end of the stream, which will only
happen on the following attempt, when the read word will be empty
and the istream::eek:perator void* will return NULL.

If you pressed ^D (on Unix, e.g.), then you might get another case
of "incorrect" behaviour because while reading "Adam's" the stream
will reach the end, and an attempt to read beyond the end of the
stream will have been made before 'while' condition is tested (which
will set the fail bit and operator void* will return NULL). In that
case "Adam's" is not going to be printed altogether.

To correctly perform word extraction one should both do testing of
the stream and checking of the word:

while (cin) {
cin >> word;
if (!word.empty())
cout << "read " << word;
}


Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,886
Members
47,419
Latest member
ArturoBres

Latest Threads

Top