Pushing words to a vector

Hello. This code is adding words to a vector. The only way to break it is to type "break". How to make it the user have to press enter instead?

1
2
3
4
5
6
7
8
	string word;
	vector<string>words;
	while (cin >> word)
	{
		if (word == "break") break;
	
		words.push_back(word);
	}
Last edited on
One possible solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;


int main()
{

  string line;
  getline(cin,line); // grabs entire line after user presses enter;
  istringstream iss(line); // put input string in stream
  vector<string> words(istream_iterator<string>(iss),istream_iterator<string>{}); // populate vector with "words"
  for(vector<string>::iterator i = words.begin(); i != words.end(); ++i)
	{
		cout << *i << endl; // display each word on its own line
	}
	return 0;
}
If the user preses enter without entering any characters, word will be empty.
5
6
7
 
  if (word.empty())
    break;
Texan40,
I meant modifying my code but thanks.
AbstractionAnon,
Doesn't work.
The only way to break it is to type "break".

Actually that is not the only way. If the user signals eof (CTRL-Z or CTRL-D) the loop will end.

I meant the only "user-friendly".
What's so "friendly" about asking the user to type "break"?

And remember by default the extraction operator skips leading white space when dealing with a string.

This code is working if this is what you want

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{	
	// Initiating vector
	vector<string>words;

	//Initiating string variable word
	string word;

	cout << "Enter words: " << endl;

	//This loops continuing inserting words via
	// words.push_back into vector containing string word
	// until break is written
	while (cin >> word)
	{
		words.push_back(word);
		if (word == "break") 
			break;	
	}

	//This for-loop keeps printing the vector until it is empty
	for(unsigned int i = 0; i < words.size(); i++)
	{
		cout << endl << words[i] << endl;
	}
}
Last edited on
jlb,
If it was friendly enough I wouldn't ask here.
patriic48,
Why are you explaining me my own code? I know what I have done. But it still won't work the way I want to.
This thread will be read by future people when they come across it googling, so he is not explaining it to you most likely.
Missed your question mighty asker, sorry for that mate but I did a few fixes for future explanationes ;)
Right. Just one note:
//This for-loop keeps printing the vector until it is empty

I don't think it will be empty.
As long as those people realize the code doesn't actually do the same thing as the OP then that would be fine. Look at the differing placement of the break statement.

Now back to topic:

If you really must have the data entry stop when the user just types the enter key you could try something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
   string word;
   vector<string>words;
   while (cin >> noskipws >> word >> skipws)
   {
      words.push_back(word);
      cin.ignore(100, '\n');
   }

   cout << "Finished" << endl;

   for(auto itr : words)
      std::cout << itr.length() << std::endl;

   return(0);
}


But beware if the user types some spaces before the "word" it may cause problems.

Well, it just pushes the first word to the vector. Also, it requires Enter to be pressed twice. I did almost the same:
1
2
3
4
5
6
7
8
9
	
string word;
	vector<string>words;
	while (cin >> word)
	{
		
		words.push_back(word);
		if(cin.get()) break;
	} 

I guess this is the right moment to ask - what exactly cin.ignore does (with these arguments)? I'm aware it is needed when using getline right after cin.
 
cin.ignore(100, '\n');


This would keep extracting characters from cin until it reaches 100 characters or encounters an endline character. This helps ensure that subsequent calls to cin aren't getting any leftover buffer contents.
Topic archived. No new replies allowed.