Vector and strings
How I can input words invector?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main() {
int n;
vector<char>words;
vector<char>substring;
cout << "Input the number of words: " << endl;
cin >> n;
cout << "Write the words: " << endl;
cin.getline(words, 256);
cout<<"write a substring"<<endl;
cin.getline(substring, 256);
}
|
1 2 3
|
std::vector<std::string> words;
for (std::string word; std::cin >> word)
words.push_back(word);
|
Signal EOF at your terminal to stop entering words.
There are a problem: expected ';' before ')' token
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int main() {
int n;
vector<string> words;
string substring;
cout << "Input the number of words: " << endl;
cin >> n;
cout << "Write the words: " << endl;
for (std::string word; std::cin >> word)
words.push_back(word);
cout << "write a substring" << endl;
cin >> substring;
}
|
Thanks. Line 8 should read
for (std::string word; std::cin >> word; )
Note the extra semicolon
Last edited on
Great, thank you!
Topic archived. No new replies allowed.