Passing in a vector that stops at a string.
Nov 8, 2017 at 4:28am UTC
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;
}
Nov 8, 2017 at 4:44am UTC
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 Nov 8, 2017 at 8:34am UTC
Nov 8, 2017 at 8:08am UTC
Shouldn't Getwords return a vector of strings ?
Nov 8, 2017 at 8:30am UTC
Good point @Thomas1965
Nov 8, 2017 at 10:12am UTC
@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 Nov 8, 2017 at 8:42pm UTC
Nov 8, 2017 at 8:33pm UTC
Thank you! This has helped me go in the right direction!
Topic archived. No new replies allowed.