I need to add a comma to each string stored in the array according to a position which is stored in the byteSizeBuffer.
std::string::iterator it;
int position = 0;
int totalSize = 0;
for (int i = 0; i < numOfMPs+1; i++)
{
for (int j = 0; j < numOfMPs; j++)
{
if(totalSize < subSize)
{
switch(byteSizeBuffer[j]) {
case 2:
position = totalSize+4; //4 because in the string each byte is 2 characters (2*2=4)
totalSize = position;
it = sub_[i].begin()+position, ','; //inserts comma at iterator+position
break;
case 4:
position = totalSize+8;
totalSize=position;
it = sub_[i].begin()+position, ',';
break;
default:
cout << "An error has occured while splitting the Data";
break;
}
}
}
}
- The position is the position which i want the comma to be inserted.
- The totalSize is the total of the positions and cannot exceed subSize
- subsize is always and even number and always 34 bytes ~68 characters
- The commas are added to the same position for every string in the array.
Strings look like: FG454F3423T5245G4G5H6546456Y54645G4456G45G60101000000101010111000001
The positions stored in the bytesizeBuffer[] are stored as either a 2 or a 4 which represent the number of bytes.
So if the numbers stored were 4,4,4,4,2,4,4,2,2,2,2 I need it to look like: FG454F34,23T5245G,4G5H6546,456Y5464,5G44,56G45G60,10100000,0101,0101,1100,0001 ..........No comma should be added at the end of the line.
My above code does not seem to work and i was wondering if i was going about it the correct way or if maybe there was a more efficient way of doing the above.
Just some tips/pointers to get me on the right track is really what i'm looking for. Thanks
bytesizeBufferconstructing a second string from the first. I have no idea why you think you need a nested loop!it = sub_[i].begin()+position, ','; //inserts comma at iterator+position. Apart from assining a new value toit, this does nothing.