Melissia_M Melissia_M - 10 months ago 60
C++ Question

Split a string entered by the user in arguments

how can I split a string entered by the User in arguments?

assuming that a user type: test1 test2 test3

how can i separate each entered values, in a way that i can access these values separately?

Answer Source

Use getline with stringstream to extract characters from user input and stores them into string until the delimitation you inform. sequentially use a vector to store the extracted characters.

something like this:

void split(std::vector<std::string> &args, std::string userInput, char delimiter) 

    std::stringstream ss(userInput);
    std::string buf;
    while(getline(ss, buf, delimiter)) {