Vector Issue

Hello,

I'm learning C++ with VS 2017 using a text book called Programming: Principles and Practices Using C++ (second ed).

I'm beginning to think there is a conflict regarding the language used. The book focuses on C++11 and perhaps C++14 is what VS wants. Though I'm speculating.

The code below is taken straight out of the text book.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include "std_lib_facilities.h"

int main()
{
    vector<string>words;
    for(string temp; cin >> temp;)
        words.push_back(temp);
    cout << "Number of words: " << words.size() << '\n';

    sort(words);

    for(int i = 0; i < words.size(); ++i)
        if (i == 0 || words[i-1] != words[i])
            cout << words[i] << '\n';

    return 0; // I have a break point at this line.
}


I'm suppose to be able to enter the following;

a man a plan a canal panama

Exactly as shown on the first line then press enter. It should then print to screen;

a
canal
man
panama
plan

The issue is that when I do this and hit enter, nothing happens. The cursor moves to the beginning of the next line, the first line has the string of text, nothing. No output.

What's missing?
Thanks.

Edit: Found an up-to-date version of std_lib_facilities.h. Removed redundant headers and change the "sort" to match the book exactly without errors. Problem still persists.
Last edited on
I think the issue is with the first loop, which never terminates.

1
2
for(string temp; cin >> temp;)
    words.push_back(temp);

This has the user enter a string and adds it to the vector, but has no exit condition. I vaguely recall mention of a terminator sequence to stop the for loop, but that may have been in a different chapter.

What chapter and section is this from?
You can easily find the solution to your ‘issue’ simply get on reading the book, i.e. reading the paragraphs after
Stroustrup wrote:
How do we stop reading string input? In other words, how do we terminate the input loop?

Thank for the advice. Yeah, I basically skipped over the vector portion.

Huh, I thought I replied to EtDecius but I don't see it, maybe I closed the tab before pressing submit.
Topic archived. No new replies allowed.