1

I've written a program that asks the user to input a number using strings, the program then will convert that number to decimal, however Im having a problem with it, when I compile (using -lm) and run the a.out, I get a Segmentation fault (core dumped), not really sure where to look or how to fix it, also one more question what do i need so that it prints the result of the conversion (printf("something..")) ?

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

int main()
{

char string[100];
int s;
char a;
char j;
int sum;

printf("B = B to D\n");
printf("D = D to B\n");
printf("choose which one to convert to:");
scanf("%c%c", &a, &j);

if (a == 'B')
{
    printf("enter binary number to convert to decimal: ");
    scanf("%s", string);

    for(s = strlen-1; s >= 0; s--)
    {

            if(string[s] == '1')
            {
            sum = sum + pow(2,s);

            }
    }

}

return 0;
1
  • strlen isn't called correctly. Commented Aug 15, 2012 at 14:11

5 Answers 5

1

You probably meant to have strlen(string) - 1, not strlen - 1. My best guess is that your program is interpreting strlen as a function pointer, and it's pretty much a given that crazy things happen after that.

As it is, you might be interested in the strtol function, which appears to do exactly what you're looking for.

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

8 Comments

Ok thanks I think that fixed it, I dont have any errors, now the last part Im stuck at is printing the decimal number that the program converted. Im not sure what to print exactly, I tried "printf("the decimal number is:", sum);" but that was just a wild guess....
Try printf("the decimal number is: %d\n", sum); - the %d represents an integer in the argument list; the \n is a new line.
yeah started just about a month ago. ALSO BIG PROBLEM, when I run this I get a HUGE number, I tried 011 and it gives me 7892986
I don't get 7892986 with your posted code and "011" input, I get 6. The same value cannot be used for the char[] subscript and the power. They should be "reversed". If subscript is 0, power should be (strlen() - (0 + 1)) or 2. If the subscript is 2, power should be (strlen() - (2 + 1)) 0r 0.
sum = sum + pow(2,string_length - (s + 1));
|
0

You use strlen as an integer. I think you mean strlen(string)

Comments

0
for(sum=0, j=0, s=strlen(string)-1; s >= 0; s--, ++j){
    if(string[s] == '1'){
        sum = sum + pow(2,j);
    }
}
printf("%d\n",sum);

Comments

0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>

void reverse_string(char *string)
{
   int string_length = strlen(string);
   char temp;
   int i;
   for (i = 0; i < string_length/2; i++)
   {
      temp = string[i];
      string[i] = string[string_length - (i + 1)];
      string[string_length - (i + 1)] = temp;
   }
}

int main()
{

   char string[100];
   int s;
   char a;
   char j;
   int sum = 0;
   int string_length = 0;
   int number, original_number;
   int remainder;
   char binary_string[200];
   int i = 0;

   printf("B = B to D\n");
   printf("D = D to B\n");
   printf("choose which one to convert to:");
   scanf("%c%c", &a, &j);
   a = toupper(a);
   if (a == 'B')
   {
      printf("enter binary number to convert to decimal: ");
      scanf("%s", string);

      string_length = strlen(string);
      for(s = strlen(string)-1; s >= 0; s--)
      {
         if(string[s] == '1')
         {
            sum = sum + pow(2,string_length - (s + 1));
         }
      }
      printf("%s in binary is %d\n",string,sum);
   }
   else if (a == 'D')
   {
      printf("enter positive decimal number to convert to binary: ");
      scanf("%s",string);
      number = atoi(string);
      original_number = number;
      if ( number < 0 )
      {
         printf("ERROR: only positive numbers please\n");
         return 1;
      }
      do
      {
         remainder = number % 2;
         if ( remainder == 0 )
            binary_string[i] = '0';
         else
            binary_string[i] = '1';
         number = number / 2;
         i += 1;
      }
      while (number > 0);
      binary_string[i] = '\0';
      reverse_string(binary_string);
      printf("decimal %d is %s in binary\n",original_number,binary_string);
   }
   return 0;
}

5 Comments

dude i think my putty is messd up... a.out, B> 11 = 6880483 I keep getting this and It wont change. even with your program.
Can you make a small program that only prints argc?
hmm I made one program that uses argc and argv but Im not too good with them.
#include <stdio.h> int main(const int argc, const char *const argv[]) { fprintf(stdout,"starting %s passed %d arguments(including pgm name)\n",argv[0],argc); }
AAAAAAAAAAAAAH! i got it!, if i initilize sum it will fix everything in my code. thank you for your help. now ill try to do decimal to binary, i dont want to just copy your program :P
0

strlen isn't used properly. I think you want to do something like strlen(string)-1. BTW your logic wont work to convert the fractional part. Check this code:

#include <stdio.h>
#define MAX 1000

int main()
{
    double fraDecimal=0.0,dFractional=0.0 ,fraFactor=0.5;
    long dIntegral = 0,bIntegral=0,bFractional[MAX];
    long intFactor=1,remainder,i=0,k=0,flag=0;
    char fraBinary[MAX];

    printf("Enter any fractional binary number: ");
    scanf("%s",&fraBinary); 
    while(fraBinary[i]) //Separating the integral and fractional parts
    {

      if(fraBinary[i] == '.')
          flag = 1; //If dot is found start taking the fractional part.
      else if(flag==0)
          bIntegral = bIntegral * 10 + (fraBinary[i] -48); 
          /* char - 48 to get the numerical value.*/
      else
          bFractional[k++] = fraBinary[i] -48;
      i++;
    }

    while(bIntegral!=0){
     remainder=bIntegral%10;
     dIntegral= dIntegral+remainder*intFactor;
     intFactor=intFactor*2;
     bIntegral=bIntegral/10;
    }

    for(i=0;i<k;i++){
     dFractional  = dFractional  + bFractional[i] * fraFactor;
     fraFactor = fraFactor / 2;
    }

    fraDecimal = dIntegral + dFractional ;

    printf("Equivalent decimal value: %Lf",fraDecimal);   
    return 0;
}

Source: C Program to Convert Binary into Decimal Number

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.