Is there an easy way to convert from Char* to int? I have tried converting as such:

char* value_a;
int* temp = value_a;
int final = temp;

The compiler is returning an error so I know something is wrong but why can't I convert this way? Any suggestions as to how to easily convert from char* to int. (Even char* to int* would get me closer) I need to be able to carry out simple calculations on the final variable. Thank you!

Why do you want to convert a character pointer to an integer? Such conversion are normally not done.

But the answer is int final = (int)value_a;

You can use also stringstream to convert to int

int final;
stringstream strm;
char * value_a = "123456";

strm << value_a;
strm >> final;

You can use also stringstream to convert to int

int final;
stringstream strm;
char * value_a = "123456";

strm << value_a;
strm >> final;

I never knew about that. This looks great

Or simply use old good atoi() from <cstdlib>:

char* value_a = "123456";
int final = std::atoi(value_a);

I think, at this level of the programming skill atoi() is a suitable solution. For example, stringsteam solution (see above) does not check the result too.
Maniacally check this, check that == not to see the wood for the trees ;)...

Why is it so hard to tell people about the best way of doing it, rather than some old hack which they'll have to unlearn sooner or later?

It's not like there's a massive difference in line count (or anything) between the two.

stringstream (the basics of it) takes no more explaining that explaining atoi would. Probably less, if you exclude all the caveats.

commented: Logic might get us not very far in this one. +9

To Salem:
Formally you are right, but...
To be honest we must write tons of words, for example:

// ... don't forget to check conversion result...
// ... better write a function ...
bool getInt(int& target, const char* source)
{
    bool ok = false;
    if (source)
    {
        std::istringstream istr(source);
        if (istr >> target)
            ok = true;
    }
    return ok;
}
// ... or use strtol() ...
char stopchar;
final = static_const<int>(std::strtol(value_a,&stopchar,10));
if (std::errno == ERANGE)
   ...
// ... or use C++ exception to signal overflow or bad data ...
   ...

And so on...
What's an awful answer to the simplest (in addition incorrectly formulated;)) question!
It seems the only result of that explanation: the burnt child dreads the fire (or C++;))...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.