Passing in a vector that stops at a string.

Write your question here.
I am trying to write a code that allows the user to type in a string. And then store that string into a vector, until the user types in "stop".
I call the vector in the first loop, but I am having trouble coding in the stop part.
I need to make two functions, one called GetWords to read in a sequence of words from cin and put them in a vector and the other, called PrintNewVector, to print them out.
I keep on getting a bunch of errors. Please I just need help coding into the right direction!

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>

using namespace std;


string GetWords() {
	string userInput;
	vector<string> newVector;
	const string STOP_LOOP = "stop";
	const int BLASTED_ONE_THOUSAND = 1000;
	string newVectorInput;
	newVectorInput == newVector;

	cout << "Please enter the words for the vector: " << endl;
	cin >> userInput;
		
        if (int i = 0; userInput< STOP_LOOP; i++){
                   

         }

	else if (userInput != STOP_LOOP||cin.fail()){
			cin.clear();
			cin.ignore(BLASTED_ONE_THOUSAND, '\n');
			cout << "Invalid selection " << endl;
		}
	}
	
	return newVector;
}


void PrintNewVector(vector<string>& words) {
	cout << words << endl;
	return;
};
          

int main () {
   vector<string> wonderful;
   
   wonderful = GetWords();
   cout << endl;
   PrintNewVector(words);
   cout << endl;
   
   return 0;
}
I didn't follow what was the intention of the existing code. This is based on the written description.
1
2
3
4
5
6
7
8
9
10
11
vector<string> GetWords() {
    cout << "Please enter the words for the vector: " << endl;
    
    string userInput;
    vector<string>  newVector;
    
    while (cin >> userInput && userInput != "stop")
        newVector.push_back(userInput);
	
    return newVector;
}


http://www.cplusplus.com/reference/vector/vector/push_back/
Last edited on
Shouldn't Getwords return a vector of strings ?
Good point @Thomas1965
@jollyHolly11, part of the problems you get have been solved by @Chervil. Your PrintNewVector() is not correct

1
2
3
4
5
6
7
8
9
10
11
12
void PrintNewVector(vector<string>& words){
    // method 1: Iterating vector elements
    for(int i = 0; i < words.size(); i++){
        cout << words[i] << endl;
    }
    
    // method 2: Range-for 
    // see also http://en.cppreference.com/w/cpp/language/range-for
    for(auto word: words){
        cout << word << endl;
    }
}

You could also do some "nice" check.

1
2
3
if the vector is empty, 
   tell the user, 
else print vector content


And in your main()
1
2
3
4
5
6
7
8
9
10
int main() {
	vector<string> wonderful;

	wonderful = GetWords();
	cout << endl;
	PrintNewVector(wonderful);
	cout << endl;

	return 0;
}


Hope the bunch of errors will now be reduced
Last edited on
Thank you! This has helped me go in the right direction!
Registered users can post here. Sign in or register to post.