-2

This is a atof function that converts string to float by first converting the string in to integer then dividing the integer number by 10s to get the real number after saving the deciaml point position

Although the condition is true, the program doesn't enter the second while loop that is responsible for omitting the decimal point

I'm using Geany 1.23.1 on ubuntu14.04

#include <stdio.h>
#include <math.h>
#include <string.h>
double atof1(char num[]);   //converts from character to float
                            // by converting string  to integer first then divide by
                            //10 to the power of the count of fraction part
int main()
{
    char test[]="123.456";
    printf("%f\n",atof1(test));

    return 0;
}

double atof1(char num[])
{
    int dp=0;               //decimal point position
    int length=strlen(num);
    int i=0;
    while(dp==0)            //increment till you find decimal point
    {
        if(num[i]=='.')
        {
            dp=i;           //decimal point found
            break;
        }
        else i++;

    }
    printf("%d %d\n",i,length);
    while(length>i);            //delete the decimal point to get integer number
    {
        num[i]=num[i+1];            //deletes deicmal point
        i++;
        printf("%d",i);
    }
    int integer_number=atoi(num); //integer number of the string
    double final =integer_number/(pow(10,(length-dp-1)));//divide by 10s to get the float according to position of the '.'

    return final;


}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Mostafa Ezz
  • 55
  • 1
  • 6

1 Answers1

3

You have an extra ; that puts the while statement in an infinite loop.

while(length>i);            //delete the decimal point to get integer number
               ^^ This is the problem.
R Sahu
  • 204,454
  • 14
  • 159
  • 270