Sign in to follow this  
TaeKwonDo

strtol() question

Recommended Posts

I just started using strtol() in my code and I am having a problem with it I basically have this:
char data[5]; //array is filled with "200"
char *pEnd;
long myLong = strtol(info,&pEnd,10);
cout<<myLong;
I got this code snippet basically off cplusplus.com my problem is though when I ouput myLong the outputted number isn't 200, its 2000. I was wondering if there is a reason for this extra '0' in my code.

Share this post


Link to post
Share on other sites
Quote:
Original post by TaeKwonDo
I just started using strtol() in my code and I am having a problem with it

I basically have this:


char data[5]; //array is filled with "200"
char *pEnd;
long myLong = strtol(info,&pEnd,10);
cout<<myLong;


I got this code snippet basically off cplusplus.com my problem is though
when I ouput myLong the outputted number isn't 200, its 2000.

I was wondering if there is a reason for this extra '0' in my code.
Is 'info' supposed to be 'data'? And can you show us where you're filling the 'data' array?

Also, you should be aware that there are better (or at least safer) methods available in C++ for performing conversions between arbitrary types and strings (e.g. boost::lexical_cast<>(), which is both safe and concise).

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
And can you show us where you're filling the 'data' array?


We're not psychic. We can't check if there's anything wrong with code we can't see.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this