Jump to content
  • Advertisement
Sign in to follow this  
TaeKwonDo

strtol() question

This topic is 3701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
yes sorry info should be data my bad, and I was filling the char array from a text file using fstream if that helps

I will also look into boost

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!