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

6
  • 1
    int* myarray = new int[sizeof(userstr)]; - why manual memory management (new) rather than using a container or (at least) a smart pointer? Commented Aug 13, 2019 at 17:26
  • Duplicate of stackoverflow.com/questions/12119645/… 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. Commented Aug 13, 2019 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. Commented Aug 13, 2019 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. ` Commented Aug 13, 2019 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? Commented Aug 13, 2019 at 17:34

2 Answers 2

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;

Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.