Edoardo La Greca Edoardo La Greca - 8 days ago 6
C++ Question

Why this split function does not work?

I was creating a function, it should split the

input1
in a
function
string (which contains the 1st word of
input1
) and in a
arguments
string (which contains the rest of
input1
). If i try to print the
function
string and the
arguments
string, they appear empty.

What should i do?

void split (string input1, string function, string arguments){

bool args = false;

string undefined;

for (int i = 0; i < input1.length(); i++){

if (input1[i] == ' '){
if (i == 0) {
input1.erase(0, 1);
} else if (i == (input1.length() - 1)){
input1.erase((input1.length() - 1), 1);
} else {
if (!args) {
for (int o = 0; o < undefined.length(); o++)
function.push_back(undefined[o]);
args = true;
} else if (args) {
for (int u = 0; u < undefined.length(); u++)
arguments.push_back(undefined[u]);
}
}

} else {
if (i == (input1.length() - 1)){
undefined.push_back(input[i]);
if (!args)
for (int u = 0; u < undefined.length(); u++)
function.push_back(undefined[u]);
else
for (int e = 0; e < undefined.length(); e++)
arguments.push_back(undefined[e]);
} else {
undefined.push_back(input[i]);
}
}
}
}

Answer

Disclaimer: This answer assumes, you're using std::string.

void split (string input1, string function, string arguments){

When this function is called like this

string a = "input";
string b;
string c;

split(a, b, c);

Then you are essentially passing copies of a, b and c to the function split. In order for function split to be able to modify those you have two options:

  1. Use pointers:

    void split (string input1, string * function, string * arguments) {
        [...]
    }
    split(a, &b, &c);
    
  2. Use references:

    void split (string input1, string & function, string & arguments) {
        [...]
    }
    split(a, b, c);
    

Option 2 hides the fact, that b and c of the caller will be modified, but is easier to implement, because in option 1 you would have to either change all uses of function or arguments to *function or *arguments or you would have to add a second reference initialisation like this (what I would prefer anyway, because it stops you from hiding the fact, that you're going to modify those params):

void split (string input1, string * _function, string * _arguments) {
    string & function = _function;
    string & arguments = _arguments;
    [...]
}

Depending on the actual task given to you, the function split might be implemented much easier than how you did it. One hint was given by PaulMcKenzie to use std::istringstream and it's operator >> to extract the words.