ex11.7 Punct_stream problem

62 views
Skip to first unread message

David Jorge

unread,
Jan 30, 2011, 4:39:22 PM1/30/11
to ppp-p...@googlegroups.com
Hi guys,
There is a problem with this exercise.

int main (int argc, char * const argv[]) {
Punct_stream ps(cin);
ps.whitespace(";:,.?!()\"{}<>");
ps.case_sensitive(false);

cout << "Please enter words\n";
vector<string> vs;
string word;

while (ps >> word)
vs.push_back(word); // read words

for (int i=0; i<vs.size(); ++i) {
if (i==0 || vs[i] != vs[i-1])cout << vs[i]<<endl;
}

return 0;
}

It keeps looping in the

> while (ps >> word)
> vs.push_back(word); // read words


and never executes the code that follows the loop.
I have done some debugging and the
> operator bool ()


is always returning true.

I have tried with the original code downloaded from the net.

I'm trying this in a mac with xcode. Maybe this is a problem related with my compiler. Anyone with this problem?

Regards,
David

Art Werschulz

unread,
Jan 30, 2011, 5:07:20 PM1/30/11
to ppp-p...@googlegroups.com
Hi.

On Jan 30, 2011, at 4:39 PM, David Jorge wrote:

> Hi guys,
> There is a problem with this exercise.
>
> int main (int argc, char * const argv[]) {
> Punct_stream ps(cin);
> ps.whitespace(";:,.?!()\"{}<>");
> ps.case_sensitive(false);
>
> cout << "Please enter words\n";
> vector<string> vs;
> string word;
>
> while (ps >> word)
> vs.push_back(word); // read words
>
> for (int i=0; i<vs.size(); ++i) {
> if (i==0 || vs[i] != vs[i-1])cout << vs[i]<<endl;
> }
>
> return 0;
> }
>
> It keeps looping in the
>
>> while (ps >> word)
>> vs.push_back(word); // read words
>
>
> and never executes the code that follows the loop.

How are you terminating the input?

Art Werschulz (8-{)} "Metaphors be with you." -- bumper sticker
GCS/M (GAT): d? -p+ c++ l u+(-) e--- m* s n+ h f g+ w+ t++ r- y?
Internet: agw STRUDEL comcast.net

David Jorge

unread,
Jan 30, 2011, 6:48:18 PM1/30/11
to ppp-p...@googlegroups.com
I'm not terminating the input, and it is not mentioned in the book but your question makes sense. Should i use a word like "END"? or "\r\n"?

> --
> You received this message because you are subscribed to the Google Groups "PPP-public" group.
> To post to this group, send email to ppp-p...@googlegroups.com.
> To unsubscribe from this group, send email to ppp-public+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ppp-public?hl=en.
>

Art Werschulz

unread,
Jan 30, 2011, 8:40:03 PM1/30/11
to ppp-p...@googlegroups.com
Hi.

On Jan 30, 2011, at 6:48 PM, David Jorge wrote:

> I'm not terminating the input, and it is not mentioned in the book but your question makes sense. Should i use a word like "END"? or "\r\n"?

In Unix, end-of-input from the terminal is generated by control-D.
In Windows, it's control-Z.

David Jorge

unread,
Feb 3, 2011, 6:25:34 PM2/3/11
to ppp-p...@googlegroups.com
I'm in a mac so it should be ctrl-D but unfortunately it doesn't work.

David Jorge

unread,
Feb 6, 2011, 7:51:17 PM2/6/11
to ppp-p...@googlegroups.com
Art, which mail should i use to send you the code?

cortical_iv

unread,
May 23, 2018, 10:15:29 AM5/23/18
to PPP-public

ctrl-d works for me I'm working in linux. Without that you will keep entering lines of text forever. :)
Reply all
Reply to author
Forward
0 new messages