tags:

views:

130

answers:

2

I'm using Stroustrup's swan book. I have run into a problem getting output from a vector. I followed the text example from sec. 4.6.3 on page 121. I managed to get the source compiled and am able to execute it. After typing in a list of whitespace separated words, the program hangs and does not list the elements of the vector as it should. I realize not every element will be outputted if it is repeated, but i receive no output at all. I have compiled and run this using the g++ 4.3.2 compiler on Linux and using the Visual C++ express 2008 compiler on windows. Both produce the same result. Thank you for taking time to read this. Here is my source:

#include "Supporting_files/std_lib_facilities.h"

    int main()
            {
        vector<string> words;
        string temp;
        cout << "Enter a list of words: ";
        while(cin>>temp)
            words.push_back(temp);
            cout << "Number of words: " << words.size() << endl;
            sort(words.begin(),words.end());
            for(int i=0;i<words.size();++i)
                if(i==0||words[i-1]!=words[i])
                    cout << words[i] << "\n";
            }
+5  A: 

while(cin>>temp) only ends when it hits an end of file. Use control-D to send an end of file into the terminal.

Todd Gardner
A: 

Thank you, all I had to do was press ctrl-d to get the program to continue.

coderasm
FYI, if someone on stackoverflow answers your question, it is polite to mark their answer as accepted (click on the check mark next to their answer).
Tyler McHenry