 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
???? Guest
|
Posted: Thu Jan 29, 2004 9:29 am Post subject: 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.
|
|
| Back to top |
|
 |
René Kjellerup Guest
|
Posted: Thu Jan 29, 2004 9:44 am Post subject: Re: code contains problem |
|
|
"????" <digitcode (AT) korea (DOT) com> skrev i en meddelelse
news:80169e17.0401290129.df1002b (AT) posting (DOT) google.com...
| Quote: | 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
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.
|
|
| Back to top |
|
 |
Martijn Lievaart Guest
|
Posted: Thu Jan 29, 2004 4:34 pm Post subject: Re: code contains problem |
|
|
On Thu, 29 Jan 2004 01:29:25 -0800, ???? wrote:
| Quote: | simple program that uses the code sequence:
#include <iostream
#include
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;".
| Quote: |
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
|
|
| Back to top |
|
 |
Nick Hounsome Guest
|
Posted: Thu Jan 29, 2004 4:56 pm Post subject: Re: code contains problem |
|
|
"????" <digitcode (AT) korea (DOT) com> wrote
| Quote: | 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
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?
| Quote: | (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.
|
|
|
| Back to top |
|
 |
MPBroida Guest
|
Posted: Thu Jan 29, 2004 10:44 pm Post subject: Re: code contains problem |
|
|
???? wrote:
| Quote: |
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
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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Jan 30, 2004 12:05 am Post subject: Re: code contains problem |
|
|
"MPBroida" <mbroida (AT) fake (DOT) domain> wrote...
| Quote: | ???? wrote:
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
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.
|
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::operator 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|