Hi....
I'm new here and i got this problem....
My function is to generate a function that is the same with atoi, just that the string library cannot be used.
eg, if the string is keyed in as "123.87", the value returned is 123.87. If "123.67.78" is keyed in, the value should be 123.67. The second decimal is ignored.
Kindly help? thanks....
Code:
#include <stdio.h>
main ()
{
char array[5];
int i;
double sum = 0;
float d= 10;
int count = 0;
printf ("Enter a string to be converted to double:\n");
for (i=0; i<5; i++)
scanf("%c", &array[i]);
if (*array>= 65 && *array <= 90 || *array >= 97 && *array<= 122) {
printf ("-32768");
}
else
{
while (*array != '\0' && count !=2)
{
if (count == 0)
sum = sum*10 +(*array - 48);
if (count == 1)
{
sum = sum + ((*array -48)/d);
d = d*10;
}
*array ++;
if (*array == '.')
{ count ++;
}
}
}
printf ("The number is %f.\n", sum);
printf ("The number of decimal is %d.\n", count);
}
Code tags added by Kermi3