473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.
Jul 22 '05 #1
5 1287

"????" <di*******@korea.com> skrev i en meddelelse
news:80*************************@posting.google.co m...
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.

Jul 22 '05 #2
On Thu, 29 Jan 2004 01:29:25 -0800, ???? wrote:
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

Jul 22 '05 #3

"????" <di*******@korea.com> wrote in message
news:80*************************@posting.google.co m...
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?
(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.

Jul 22 '05 #4
???? 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 <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
Jul 22 '05 #5
"MPBroida" <mb*****@fake.domain> wrote...
???? 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 <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.


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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Trevor Fairchild | last post by:
I'm trying to execute a full-text query from a vb.net web application. The problem I have is that in SQL Server, the syntax for a full-text search is SELECT * FROM table WHERE CONTAINS( *, '...
8
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I haven't been able to figure out even what key...
3
by: Earthling | last post by:
Any help would be appreciated to solve the following simple problem that I will describe. *** There is a form called "red chocolate form". The form has a particular subform field that has a...
1
by: John | last post by:
Hi First of all apologies for posting so much code but this is rather involved and I am totally stumped. Basically I have a main form (Staff Batch SMS/E-Mail) which calls a function (SendSMS) in...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
by: Jedufa | last post by:
following of thread: "Adding namespaces to code behind automatically" Hello, I had quite the same problem and got further in the right direction with your suggestions, thanks. Nevertheless, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.