[Solved] C++ convert *string to int [closed]


@shivam-gupta hi buddy,

You maybe have been quite new to C/C++ programming is that correct? Some people at the forum require you to make explain what your problem is in detail also with the right C++ syntax.

I know what you mean, and it had been a main problem with me in the beginning when I tried to learn C/C++. The function that you might be looking at is atol() not atoi(). From my point of view it is better to convert the string to double, then cast it to a long/integer variable.

The code I used to complete your snippet is this

#include <iostream>

long valid(const char* str);

int main()
{
    long test;
    const char *str = "9789070002046";  //13 digit long

    test = valid(str);

    std::cout << test << " sizeof test is " << sizeof(test) << std::endl;

    return 0;
}

long valid(const char* str)
{
    long temp = (long)atol(str);
    return temp;
}

Enjoy coding C++! And remember, life is not too short to learn C++!

solved C++ convert *string to int [closed]