strtol() question

Started by
2 comments, last by Zahlman 15 years, 11 months ago
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.
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).
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
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.

This topic is closed to new replies.

Advertisement