string to int

Hi everyone, i am C++ newbie.

I want to ask how can i convert string to int and split it?

Like given string input is 00;
I want it to become 2 int which is 0 and 0...

izit possible?
It is possible, but you have to have a more explicit ruleset than just "like".
closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# include <iostream>
# include <cstdlib>// for atoi function
# include <cmath>//for floor , log10 pow functions
using namespace std;

int main()
{
    char stringNbr[] = "57";
    int nbr = atoi(stringNbr);// The conversion

    cout << nbr << endl;

    int nbrFloor = floor(log10(nbr));

    for(int i=nbrFloor;i>=0;i--)// The spliting
    {
        const int tmpNum = pow(10,i);
        int numResult = nbr/tmpNum;

        cout << numResult << endl;

        nbr -= (numResult*tmpNum);
    }

    return 0;
}


You could do it easily by directly split the stringNbr nbr the convert it to an int

hope that helps
1
2
3
4
5
char s[] = "012345";
int a[sizeof( s ) -1];

std::transform( std::begin( s ), std::end( s ) -1, std::begin( a ),
                []( char c ) { return ( c - '0' ); } ); 
Last edited on
Topic archived. No new replies allowed.