string tokenizer

i have a problem with my following code, note: this is just part of my code
when i run it, lets say i enter hello world
it will output:
hello
segmentation fault

is there a way i can fix this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void input::read_line()
{
  string command;
  while(command != "exit")
    {
      cout <<"Enter a command" << endl;
      cin >> command;
      string buf;
      stringstream ss(command);

      vector<string> tokens;
      while(ss >> buf)
        tokens.push_back(buf);

      cout << tokens[0] << endl;
      cout << tokens[1] << endl;
    }
You are assuming there will be at least two tokens in the 'tokens' vector. You must never make such assumptions, especially when coming from user input!
You're getting the segmentation fault because tokens[1] doesn't exist. The array is only 1 element big because you only put "hello" in it.

Remember the >> operator stops at whitespace, so when you say cin >> command you are only taking up to the first space... which is why you only get "hello". If you want to get the entire line, use getline:

 
getline( cin, command );


Also, you should never even allow the possibility of accessing outside of array bounds. If the user inputs fewer than 2 tokens, your program will fail. Check the size of tokens before you print, and make sure you never print past the end of the vector.
how could i fix that for any number of tokens?
use a for loop. Loop from zero up to the size of the vector (ie: tokens.size()).
ok so i changed it but im still getting a segmentation fault
i did the following input:
hello all of you
and i get the following output
hello
all
of
you
segmentation fault

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 string command;
  while(command != "exit")
    {

      cout <<"Enter a command" << endl;
      getline(cin, command);
      string buf;
      stringstream ss(command);

      vector<string> tokens;
      while(ss >> buf)
        tokens.push_back(buf);

      for(int i = 0; tokens.size();i ++)
        cout << tokens[i] << endl;
Last edited on
i actually dont need to print anything, it was just a test

i want to do something like this:
1
2
3
4
5
6
7

if(tokens[2] == "dirs")
{
//do something
}



i keep getting segmentation fault
Probably because you don't compare tokens.size() to anything.

for(int i = 0; tokens.size();i ++) makes no sense
i meant for(int i = 0; i < tokens.size();i++)
i dont need to print anything but how would i handle if the input is less than 2 tokens

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

string command;
  while(command != "exit")
    {
      cout <<"Enter a command" << endl;
      getline(cin, command);
      string buf;
      stringstream ss(command);
      vector<string> tokens;

      while(ss >> buf)
        tokens.push_back(buf);

      if(tokens[0] == "cd")
        {
// do something
        }
}
//.. 
if you aren't printing anything, then what is there to handle?
1
2
3
4
if(tokens[0] == "cd")
        {
// i call a function here 
        }


so when the input is just one token "cd" it returns the home directory.
but if it has 2 inputs like cd hello it will go to that directory.
so the problem is that when i only input one token it gives me a segmentation fault

so basically what im trying to do is parse a command line and its arguments from user input
Last edited on
Topic archived. No new replies allowed.