How can I convert a string to int and char, with fixed number of positions, in C++? For example: I need to convert "A1920139" into char "A", int 192 (next three positions in the string), int 01 (following two positions), and int 39 (last two positions).
So far I have only managed to get each int by itself (1, 9, 2, 0, etc). I don't know how to get the char or define a fixed number of positions for the int. This is what I have managed to write:
string userstr;
int* myarray = new int[sizeof(userstr)];
userstr = "A1920139";
for (i = 1; i < userstr.size(); i++) {
myarray[i] = userstr[i] - '0';
}
for (i = 1; i < userstr.size(); i++) {
printf("Number %d: %d\n", i, myarray[i]);
}
Eventually I need to arrive at something like char="A", int_1=192, int_2=01, and int_3=39
int* myarray = new int[sizeof(userstr)];- why manual memory management (new) rather than using a container or (at least) a smart pointer?new int[sizeof(userstr)];. But since OP used.size()later, it may just be a typo.int* myarray = new int[userstr.size()];,userstr's size will be zero at that point. It hasn't been assigned anything yet. `