is it wrong? : simple C++ question

Started by
21 comments, last by Hodgman 15 years, 8 months ago
I've been going going back over C++ after taking a course in C++ and Java a month or two ago. I've been messing around in Java but I figured it was time to go back to C++ and work up to using APIs and getting off console programs. But

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string word;
    
    while ( cin >> word )
		cout << "word read is: " << word << "\n";
	
	cout << "ok: no more words to read: bye!\n";
    return 0;
}
According to my book (C++ Primer) this should output when I type in: " riverrun, past Eve and Adam's "

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!
I get everything except the last line. The loop obviously never ends, it just sits there and waits for more words to be entered. According to the book its right, but I don't see how. Is there a typo or am I missing something?
Advertisement
I'm not too experienced with C++ but I think that your problem is this:

cin >> word does not return a boolean value therefore your condition is automatically evaluated as true running the loop for ever.
J.W.
Try this:

while ( cin >> word && word != "quit" )	cout << "The word is:  " << word << "\n";


...with " riverrun, past Eve and Adam's quit "
I agree with you, but my book does not.

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


I'm not one to argue with a book, I don't know enough to like pretty much see it and know if it's wrong. Anyone know if I'm missing something or the segment is just wrong?
[Heh, two people posted in while I was writing.]

You COULD change that to

while( cin >> word, word != exitValue )

I used things like this a lot in school, but not so much anymore. It's not really the safest code you could write, but it works.

The comma operator makes it so the first statement runs, then is forgotten for the second. The above suggestion of using &&, however, catches a few problems I never cared to worry about.

jdub:
> cin >> word does not return a boolean value

Well...filestreams actually return a void pointer via implicit conversion when inside if statements (zero for bad stream, non-zero for file opened). Maybe cin does something like that as well. Not that my own testing showed that was the case, though.
Quote:Original post by _fastcall
Try this:

*** Source Snippet Removed ***

...with " riverrun, past Eve and Adam's quit "



Yeah, that works but I just don't understand how the book could miss something like that. Oh well, I'll just chalk it up to bad editing.

Thank you for the help.
Quote:Original post by sjmx22
Quote:Original post by _fastcall
Try this:

*** Source Snippet Removed ***

...with " riverrun, past Eve and Adam's quit "



Yeah, that works but I just don't understand how the book could miss something like that. Oh well, I'll just chalk it up to bad editing.

Thank you for the help.
I don't think the book missed anything; in this case, operator>>() returns a reference to the input stream, and (as mentioned) the input stream can be implicitly converted to a boolean value, so the original code does in fact make sense as written.

You might give the text another read - it may be that you're supposed to enter an end-of-file code of some sort (e.g. ctrl-Z) to terminate the loop.
perhaps hitting CTRL+C will set the stream to EOF and the conversion may yield false, but its been so long since I wrote console based programs, so try that.
Yeah the code is correct. But it's assuming that you're going to type in an EOF character (this used to be common knowledge when UNIX was king).

Try ctrl+C or ctrl+D or ctrl+Z or something...
This is the exact excerpt from the book. Doesn't mention anything about end of file code or terminating the loop.

The code sequencestring 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'sWhen these words are entered at the keyboard, the output of the program is as follows:word read is: riverrun,word read is: pastword read is: Eveword read is: andword read is: Adam'sword read is: ok: no more words to read: bye!



Quote:Original post by Hodgman
Yeah the code is correct. But it's assuming that you're going to type in an EOF character (this used to be common knowledge when UNIX was king).

Try ctrl+C or ctrl+D or ctrl+Z or something...



Ctrl+C ends the program and cuts off the command line so it says,

wor^C

Ctrl+Z causes an endless loop that needs to be ended with ctrl+C and ctrl+D is a special character.


Ctrl+C seems to be what it needs, it definitely ends the loop but it can't output the full line.


I'm using Windows, not Unix. The book didn't specificly say, use UNIX and is usually worded with instructions for both. There wasn't a special instruction with this though.

This topic is closed to new replies.

Advertisement