0
ans = raw_input("Input: ")
if ans.split()[0] == "hello":
    something.add(ans.split()[1],ans.split()[2])
else if ans.split()[0] == "no":
    else.add(ans.split()[1],ans.split()[2])
/*
So if i would input "Hello peter pan"
Then it would send in >> something.add("peter", "pan")
or
if i would input "no test 123"
Then it would send in >> else.add("test", "123")
*/

So what i want to do is "catch" every word from the input but i can't seem to find any easy way to do this function in c++. I'm really new to C++, any tips?

Widdin
  • 47
  • 1
  • 4
  • 2
    http://stackoverflow.com/questions/236129/split-a-string-in-c , http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c , and http://stackoverflow.com/questions/16286095/similar-function-to-javas-string-split-in-c would be questions that may be useful to consider as they are quite similar to what you're asking here. – JB King Nov 18 '15 at 21:12
  • It's hard to understand those, i don't know how to get out the 2nd and 3rd word and use them either from the others, They only pick out the 1st word and use it – Widdin Nov 18 '15 at 21:30
  • http://www.cplusplus.com/reference/cstring/strtok/ would seem to be what you need though this function is used slightly differently. If that doesn't work please state what part you are having problems with as "hard to understand" doesn't give anyone here something to use in helping you if that is really want you want. – JB King Nov 18 '15 at 22:08
  • How do i use this correctly? http://stackoverflow.com/questions/236129/split-a-string-in-c/5208977#5208977 – Widdin Nov 18 '15 at 22:49
  • `while (ss >> buf) { tokens.push_back(buf); if (buf == "add") { cout << "add was found"; dosomething.add(tokens(1),tokens(2)); <- How to do this also? } }` – Widdin Nov 18 '15 at 22:49
  • after the `while` loop finishes so that the tokens are there then you can use them. Otherwise you are trying to access stuff that isn't properly set up. – JB King Nov 18 '15 at 23:33
  • So the if( ) is supposed to be after the while-loop? :) – Widdin Nov 18 '15 at 23:48

0 Answers0