Comma Separated String to int pointer and length

Hi,

I've done a search for around 1/2 hour now and without success. I'm trying to convert a comma separated string into an int array and the number of integers.

The input may be 3, 3,4, 5, 6, 57

So the number of spaces can be 0 or more.

The return is a integer pointer and an int with the number of integers

Any help greatly appreciated.

Paul
Post ur code
Hi,

I'm afraid I can't produce the code as all I've got so far is:

struct intarray {
int * intp;
int intlgh;
};

intarray csvint(string* ptinput){


int unsigned pos = 0, frompos = 0;
intarray out;
pos = ptinput->find(',');
while(pos != string::npos){

}

return out;
}

This is now my code, not error proof at all.

struct intarray {
int * intp;
int intlgh;
};

int commano(string * ptinput){

int unsigned pos = 0, frompos = 0;
int out = 0;
pos = ptinput->find(',');
while(pos != string::npos){
out++;
frompos = pos + 1;
pos = ptinput->find(',', frompos);
}
return ++out;
}
intarray csvint(string* ptinput){

int unsigned pos1 = 0, pos2 = 0, i = 0;
string sub;

int noints = commano(ptinput);
int * ints = new int[noints];

pos2 = ptinput->find(',');
while(pos2 != string::npos){
sub = ptinput->substr(pos1, pos2 - pos1);
ints[i] = stringint(&sub);
pos1 = pos2 + 1;
pos2 = ptinput->find(',', pos1);
i++;
}
ints[i] = stringint(&ptinput->substr(pos1));
intarray result = {ints, noints};
return result;
}
int stringint(string * input){
int result;
stringstream myStream(*input);
myStream >> result;
return result;
}

Any suggestions or criticism very welcome

Topic archived. No new replies allowed.