0

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

  • 1
    `int* myarray = new int[sizeof(userstr)];` - why manual memory management (`new`) rather than using a container or (at least) a smart pointer? – Jesper Juhl Aug 13 '19 at 17:26
  • Duplicate of https://stackoverflow.com/questions/12119645/sizeof-in-c-showing-string-size-one-less I can't close because I already accidentally closed with the wrong link. This is regarding `new int[sizeof(userstr)];`. But since OP used `.size()` later, it may just be a typo. – François Andrieux Aug 13 '19 at 17:26
  • On second though, it seems the question is more broad than that. OP is trying to group the string into batches of digits and may not have noticed the effects of undefined behavior yet. The duplicate only addresses a problem with the code but it's not what is being asked. – François Andrieux Aug 13 '19 at 17:28
  • Another note: Even if you change to `int* myarray = new int[userstr.size()];`, `userstr`'s size will be zero at that point. It hasn't been assigned anything yet. ` – user4581301 Aug 13 '19 at 17:29
  • how is my question duplicate of that in the link? I don't see an answer to my question in that link - it only relates to getting the size of a string? – Carlos Martins Aug 13 '19 at 17:34
  • @Carlos you're right.and François admits this in comment three. It solves the immediate problem of the the size of the string object, rather than the size of the string it represents, being used to size the array. You probably haven't even noticed this mistake yet because `sizeof (std::string)` is larger than the data you currently want to store. – user4581301 Aug 13 '19 at 17:38

2 Answers2

2

If I understand correctly, you want to parse a string at fixed positions, in that case you can use substr() and stoi() like this:

std::string userstr = "A1920139";
int myarray[3];

myarray[0] = std::stoi(userstr.substr(1, 3));
myarray[1] = std::stoi(userstr.substr(4, 2));
myarray[2] = std::stoi(userstr.substr(6, 2));

std::cout << "int_1=" << myarray[0] << ", int_2=" << myarray[1] << ", int_3=" << myarray[2] << std::endl;

rustyx
  • 80,671
  • 25
  • 200
  • 267
1

substr(pos, len) will get a substring starting at position pos of length len.

stoi will convert a string to an integer.

#include <string>
#include <iostream>

int main()
{
    std::string userstr = "A1920139";

    char c = userstr[0];
    int n1 = std::stoi(userstr.substr(1, 3));
    int n2 = std::stoi(userstr.substr(4, 2));
    int n3 = std::stoi(userstr.substr(6, 2));

    std::cout << c << ' ' << n1 << ' ' << n2 << ' ' << n3 << std::endl;
}
Wyck
  • 10,311
  • 6
  • 39
  • 60