convert cstring array to int array

Oct 26, 2009 at 3:06am
forum,

i have been working on this for quite sometime with different codes but i still cant convert an array of cstring values to array on integer values.

int num[300];
cstring save_slice_no[300];

for(int slice = 0; slice < 301; slice++)
{
num[slice] = sav_slice_no[slice];
num[slice] = int.Parse(sav_slice_no[slice]);
}

thanks!
Oct 26, 2009 at 3:33am
(Assuming typedef char* cstring;)

Arrays go from 0-(end-1), so those arrays you have go from 0-299, not 300.

num[slice] = int.Parse(sav_slice_no[slice]);
??? What is this?

Anyway, use a cast:

num[slice] = static_cast<int>(sav_slice_no[slice]);


You will have to either use something like atoi() (nonstandard IIRC), or parse it yourself. If you are using C++, you could use a stringstream to make it easier.
Last edited on Oct 26, 2009 at 4:05am
Oct 26, 2009 at 3:43am
Tell me you're not actually suggesting using static_cast<> to parse a string to an integer. Or at least tell me you see the problem with using a static cast to convert a value available only at run time.
Oct 26, 2009 at 4:02am
Er, whoops, I thought they were trying to convert an array of chars to an array of ints >_>
Oct 26, 2009 at 7:43pm
Topic archived. No new replies allowed.