What Am I Missing?

I have an area within my program that the user will need to input a list of words and once done use ctrl-z to end the list. I then want to take the list of words that the user enters and have a function do something to that list of words. I am not sure if it's my input or my function that I am having trouble with. It's not allowing me to use the whole list the user entered...just the last word that was entered. I think I need more to my while loop but I'm just not sure what else to put here. Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13

    cout<<"Word? (or CTRL Z to end)";
     while (cin>>word)
     {
        cout<<"Word? ";
        
     }   
     
     //function call is here in my code



I kind of doubt the console accepts CTRL-Z in any way. More logically, do this:

1
2
3
4
5
string word = "";
do {
    cout << "Next word, or END to quit? ";
    cin >> word;
} while (word != "END");
1
2
3
4
5
6
7
cout<<"Word? (or CTRL Z to end)";
cin >> word;
while(!cin.eof())
{
  cout << "Word? ";
  cin>>word;
}


In windows, CTRL+Z puts an end of file in the stream.
Last edited on
In a Unix-like terminal Ctrl+Z stops a program while Ctrl+D sends an EOF. I believe Gaminic solution to be more universally effective but change the word END to EOF which avoids the chance of the user needing the word END and being put in an odd position.
I noticed none of you answered the actual question. *cough*

It's not allowing me to use the whole list the user entered...just the last word that was entered.


Well, that's to be expected. You don't have a list there. You just have the last word that was entered.

So, you need some kind of container for your words. Something like:

1
2
3
4
5
6
7
8
9
10
     std::list<std::string> word_list; 

     cout << "Word or CTRL Z to end)";
     while ( cin >> word )
     {
          word_list.push_back(word) ;
          cout << "Word? ";
     }

     // manipulate word_list here. 


perhaps.

More info on std::list here: http://www.cplusplus.com/reference/stl/list/
Last edited on
none of you answered the actual question
Ouch, aye.
Topic archived. No new replies allowed.