Why I can't print third element of my vector ? I'm need a words that will be divided to vector and after that, they will be printed. But my third element of vector isn't showing in the terminal.
#include <iostream>
#include <vector>
using namespace std;
void divideTextCommand(string txt, vector<string> cmd)
{
string word = "";
for (auto x : txt)
{
if (x == ' ')
{
cmd.push_back(word);
word = "";
}
else
{
word = word + x;
}
}
cmd.push_back(word);
}
int main()
{
cout << "Command:" << endl;
string textCommand; getline(cin, textCommand);
vector<string> command;
cout << " " << endl;
divideTextCommand(textCommand, command);
cout << command[2];
return 0;
}
In my terminal program looks like this
bob@msi:~/Desktop/project$ ./output
Command:
barry jack fred john
Segmentation fault (core dumped)