wyrmmage
2007.06.22, 04:19 PM
A couple of months ago, I was googling around looking for a way to turn an array of chars into either a double or an int. I looked through two pages of google search results (most were forums), but couldn't find anything except for 'you have to make your own function'. This seemed a bit odd to me, since I figured that it must be a fairly common problem. Anyway, I went ahead and built my own functions for it:
int makeInt(char* theString)
{
int theNumber = 0;
int theLength = strlen(theString)-1;
bool isNegative = 0;
if(theString[0]=='-')
{
isNegative = 1;
for(int i=0; i<theLength; i++)
{
theString[i] = theString[i+1];
}
theLength = theLength-1;
}
int tempNum = 10;
for(int x=0; x<=theLength; x++)
{
for(int i=0; i<=9; i++)
{
if(theString[x]==theNumbers[i])
{
for(int z=0; z<=theLength-x-1; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber + (i * (tempNum/10));
tempNum = 10;
}
}
}
if(isNegative)
{
theNumber = theNumber * -1;
}
return theNumber;
}
and
double makeDouble(char* theString)
{
double theNumber = 0.0;
int theLength = strlen(theString)-1;
int dotPos = -1;
bool isNegative = 0;
if(theString[0]=='-')
{
isNegative = 1;
for(int i=0; i<theLength; i++)
{
theString[i] = theString[i+1];
}
theLength = theLength-1;
}
for(int i=0; i<=theLength; i++)
{
if(theString[i]=='.')
{
dotPos = i;
}
else
{
if(dotPos != -1)
{
theString[i-1] = theString[i];
if(i==theLength)
{
theString[i]='\0';
theLength = theLength - 1;
}
}
}
}
int tempNum = 10;
for(int x=0; x<=theLength; x++)
{
for(int i=0; i<=9; i++)
{
if(theString[x]==theNumbers[i])
{
for(int z=0; z<=theLength-x-1; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber + (i * (tempNum/10));
tempNum = 10;
}
}
}
if(dotPos != -1)
{
int tempNum = 10;
for(int z=1; z<=dotPos; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber / (tempNum/10);
}
if(isNegative)
{
theNumber = theNumber * -1;
}
return theNumber;
}
These functions worked fine, but I was always a little uncomfortable with them, figuring that they were slow and what not. Last week, I ran across these functions:
int atoi( const char *str );
double atof( const char *str );
int sprintf( char *buffer, const char *format, ... );
Why does no one recommend these? Is there something wrong with them...are they not very accurate or something, or is it just because they're C, so some C++ people think that they're 'wrong'? :\
Thanks guys :)
-wyrmmage
int makeInt(char* theString)
{
int theNumber = 0;
int theLength = strlen(theString)-1;
bool isNegative = 0;
if(theString[0]=='-')
{
isNegative = 1;
for(int i=0; i<theLength; i++)
{
theString[i] = theString[i+1];
}
theLength = theLength-1;
}
int tempNum = 10;
for(int x=0; x<=theLength; x++)
{
for(int i=0; i<=9; i++)
{
if(theString[x]==theNumbers[i])
{
for(int z=0; z<=theLength-x-1; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber + (i * (tempNum/10));
tempNum = 10;
}
}
}
if(isNegative)
{
theNumber = theNumber * -1;
}
return theNumber;
}
and
double makeDouble(char* theString)
{
double theNumber = 0.0;
int theLength = strlen(theString)-1;
int dotPos = -1;
bool isNegative = 0;
if(theString[0]=='-')
{
isNegative = 1;
for(int i=0; i<theLength; i++)
{
theString[i] = theString[i+1];
}
theLength = theLength-1;
}
for(int i=0; i<=theLength; i++)
{
if(theString[i]=='.')
{
dotPos = i;
}
else
{
if(dotPos != -1)
{
theString[i-1] = theString[i];
if(i==theLength)
{
theString[i]='\0';
theLength = theLength - 1;
}
}
}
}
int tempNum = 10;
for(int x=0; x<=theLength; x++)
{
for(int i=0; i<=9; i++)
{
if(theString[x]==theNumbers[i])
{
for(int z=0; z<=theLength-x-1; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber + (i * (tempNum/10));
tempNum = 10;
}
}
}
if(dotPos != -1)
{
int tempNum = 10;
for(int z=1; z<=dotPos; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber / (tempNum/10);
}
if(isNegative)
{
theNumber = theNumber * -1;
}
return theNumber;
}
These functions worked fine, but I was always a little uncomfortable with them, figuring that they were slow and what not. Last week, I ran across these functions:
int atoi( const char *str );
double atof( const char *str );
int sprintf( char *buffer, const char *format, ... );
Why does no one recommend these? Is there something wrong with them...are they not very accurate or something, or is it just because they're C, so some C++ people think that they're 'wrong'? :\
Thanks guys :)
-wyrmmage