-4

Its a program to convert integer stored in a string into an int. Please help.

 #include<stdio.h>
 #include<math.h>
 int main()
{
int num, n, i;
num = 0;
printf("Enter n\n");
scanf("%d", &n);
char string[n+1];
printf("Enter the number\n");
scanf("%s", &string);
for(i=0;string[i]!='\0';i++)
{
    num = num + string[i]*pow(10,n-i-1);
}
printf("The required number is %d", num);
return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 3
    The debugger is your friend. Step through the program with the debugger that came with your development tools and keep an eye out for where the program does something unexpected like store the wrong value or take the wrong path. The unexpected is almost always a bug. The rest of the time it's incorrect expectations and that also needs to be fixed. – user4581301 Jan 06 '21 at 17:27
  • A warning about `pow`: `pow` operates on floating point numbers and [floating point numbers are imprecise](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). You think you got 100 but sometimes you really get 99.999999999, and when converted to an integer the fractional portion is not rounded, it's discarded and you wind up with 99. – user4581301 Jan 06 '21 at 17:30
  • Note: There is little point to a variable length `string`. `int` can only represent a limited number of digits, typically 10, so allowing the user to input a string longer than 10 digits will result in an incorrect answer. – user4581301 Jan 06 '21 at 17:51

2 Answers2

2

In typical environment, character codes for digits are not equal to the numbers the digits represent for.

Character codes for digits in C are defined to be continuous, so you can convert the character codes to the corresponding numbers by subtracting '0' from the character code.

    num = num + (string[i]-'0')*pow(10,n-i-1);

By the way, there are some better ways to do the conversion:

sscanf(string, "%d", &num); /* available in stdio.h */
num = atoi(string); /* stdlib.h is required for atoi() */

Also note that scanf("%s", &string); invokes undefined behavior because a pointer to array is passed where char* (a pointer to char) is required. The & should be removed.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

In your code the size of your string must be equal to n+1 ,so you can removed all this

printf("Enter n\n");
scanf("%d", &n);
char string[n+1];

for(int i=strlen(string)-1;i>=0;i--)

I started from the last caracter in my string to the first caracter and I multiplied each caracter by j (j=1 then j=10 then j=100 .....)

#include<stdio.h>
#include<math.h>

int main()
{
   int j=1;
   char string[100];
   printf("Enter the number\n");
   scanf(" %s", string);

   int num=0;

   for(int i=strlen(string)-1;i>=0;i--)
   {
       int k=string[i]-'0';//subtract each case by '0'
       num=num+k*j;
       j=j*10;
   }
   printf("The required number is %d", num);
   return 0;
}
MED LDN
  • 684
  • 1
  • 5
  • 10
  • Is my answer false ? – MED LDN Jan 06 '21 at 17:49
  • No. Just slower than it needs to be and could use a bit of input validation to ensure the user doesn't type in letters. Also remember that `int` probably can't hold more than 10 or 20 digits, so allowing the user to feed in more than that will result in errors. Explanation is now sufficient. – user4581301 Jan 06 '21 at 17:53