Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need suggestion to put error handler.

Status
Not open for further replies.

amarg

Programmer
Nov 19, 2002
106
0
0
IN
Hi,

I wrote a small code to convert string into double. Code is as follow

double stof(char *i_str)
{
char *firstPart = NULL;
char *secondPart = NULL;
double outPut;


if(i_str[0] != '.') {

firstPart = strtok(i_str, ".");
secondPart = &i_str[ strlen(firstPart) + 1 ];
outPut = (double)atoi(firstPart) + ((double)atoi(secondPart)) / (pow(10, strlen(secondPart)));

}
else {

firstPart = '\0';
secondPart = &i_str[ 1 ];
outPut = 0.0 + ((double)atoi(secondPart)) / (pow(10, strlen(secondPart)));

}

return outPut;

}

It's taking a sting input like "102.433" and returning a double having a value 102.433. But there is no error Handle in this function. Can anyone suggest me to put some error handler in that function with different type of inputs( valid or invalid )...


--Amar
 
Use the strtod function defined in the stdlib.h

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top