Public Group

# is it wrong? : simple C++ question

This topic is 3700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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,

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?

##### Share on other sites
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.

##### Share on other sites
Try this:

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

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

##### Share on other sites
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?

##### Share on other sites
[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.

##### Share on other sites
Quote:
 Original post by _fastcallTry 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.

##### Share on other sites
Quote:
Original post by sjmx22
Quote:
 Original post by _fastcallTry 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.

##### Share on other sites
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.

##### Share on other sites
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...

##### Share on other sites
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 HodgmanYeah 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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
frob
11

• 9
• 19
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007457

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!