0

The code I have written is as follows:

#include<stdio.h>
#include<math.h>
#include<string.h>
int strToint(char []);
int main()
{
    char str[20];
    printf("Enter the string-");
    gets(str);
    printf("\nthe integer is %d ",strToint(str));
    return 0;
}
int strToint(char str[])
{
    int i,a,sum=0,k,j;
    a=strlen(str);
    for(i=0;i<a;i++)
    {
        k=str[i]-'0';
        j=pow(10,a-i-1);
        sum+=k*j;
    }
    return sum;
}

If I enter the input as, say 567, I get 562 but I do not see why. I feel this is probably related to the pow function.

Help is appreciated. Thanks!

9
  • you simply can use aoit function for this conversion... sum=atoi(str); Commented Sep 28, 2014 at 19:59
  • 1
    I get a correct answer: ideone.com/4Ae89x Commented Sep 28, 2014 at 19:59
  • For input "567" i get output 567 with your code. Everything seems to work. Commented Sep 28, 2014 at 20:00
  • What's wrong with sscanf? And why would you call strlen() and so iterate over the string twice? And why call pow() which is a floating point function. Use integer arithmetic if you must do this. And what if your string doesn't contain all digit characters? Commented Sep 28, 2014 at 20:00
  • @BartlomiejLewandowski: Here's a screenshot of what I see: i58.tinypic.com/2hdteua.png . Any idea what's happening? :/ Commented Sep 28, 2014 at 20:03

3 Answers 3

2

pow() is a floating point function and its behaviour varies from system to system. There's no requirement placed by the standard on its accuracy. It seems that yours is not returning values with the accuracy that you would require.

You do not need to implement functions to convert text to integer since the standard library contains functions that do that. An obvious choice would be sscanf.

But if you must implement such a function, you should use integer arithmetic. Any such code should handle negative values, and check for invalid input. For instance. What happens when you pass "a" to your function?

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks David! I have made the code assuming that the user enters a valid input.
pow is required to return 1.0 if the second argument is 0. So it seems that this is not a conforming C implementation.
@Jens I rather imagined that it was the other calls to pow that would fail. All the same, floating point is wrong here.
@Jens Gustedt Confident OP's pow() did return 1.0 with pow(10,0), but returned 99.9999.. on pow(10,2).
2

I tested your code and got the expected result. Maybe the problem is related with function pow.

You could write the function simpler without using functions that deal with real numbers.

For example

int strToint( const char str[] )
{
    int sum = 0;

    for ( ; *str; ++str )
    {
        sum = 10 * sum + *str - '0';
    }

    return sum;
}

Take into account that function gets is unsafe. It is better to use function fgets. For example

fgets( str, 20, stdin );

size_t n = strlen( str );
if ( str[n - 1] == '\n' ) str[n - 1] = '\0';

1 Comment

Thank you again, I did not know about fgets. :)
1

On the first call to pow(10,2) the library's weak pow() returns 99.999... which is truncated to 99 on assignment to j.

Since the first digit is 5, the final sum is 5 less. (100*5 vs 99*5)

If code must use FP functions, best to round before integer assignment

j = round(pow(10,...));

As @Vlad points out, all FP math can be avoided.

--

Floating point functions are not required by C to be accurate. A good pow() function though should have provided an exact answer here, that is why it worked on so many other machines.

For a complete atoi, there are many posts, such as FromStringToInteger

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.