Error: Decimal to BCD Conversion

This is a discussion on Error: Decimal to BCD Conversion within the C Programming forums, part of the General Programming Boards category; Dear All, I have written a program for decimal to BCD conversion of numbers in C (Dev C++ IDE). My ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Error: Decimal to BCD Conversion

    Dear All,

    I have written a program for decimal to BCD conversion of numbers in C (Dev C++ IDE). My code hangs after execution, I have debugged the code but found no errors.

    Kindly check the below code and point out the errors:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int *dectobin(int dig);
    int main()
    {
        int n,dig,i=0;
        int bcd[100]={0},b[100]={0};
        printf("Enter the number to be converted into BCD");
        scanf("%d",&n);
        while(n!=0)
        {
                   dig=n%10;
                   n=n/10;
                   b[i]=dectobin(dig);
                   bcd[i]=b[i];
                   i=i+1;
        }          
                   for(;i>=0;i--)
        {
                   printf("%d/t",bcd[i]);    
        }
                   system("pause");
                   return 0;
    }
    int *dectobin(int dig)
    {
        int r,i=0,j=0;
        int array[100]={0};
        static int reversearray[100]={0};
        while(dig!=0)
        {
        array[i]=dig%2;
        dig=dig/2;
        i=i+1;
        }
        for(;i>=0;i++)
        {
             reversearray[j]=array[i];
             j+=1;
        }
        return reversearray;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    When I compiled your code I got the following error:
    In function ‘main’:|
    main.c|14|error: assignment makes integer from pointer without a cast|
    And this should be line 14:
    Code:
                   b[i]=dectobin(dig);
    Your function is returning a pointer, you can not assign a pointer to an int.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    @jim - Thank you for your reply. I wanted to ask you if you can suggest a way to counter this error?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    What type of variable are you returning from your function? What is the type of the variable that you are trying to store that variable into? Why are you trying to store a pointer into an int (bcd[i])?

    Jim

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    I am using the dectobin function to input a decimal number and convert it into binary form. I am returning a 4 digit binary coded representation of a single integer from my function. I am trying to store the variable into an array b[i], here b[i] represents an array with each element containing the BCD representation of individual digits of the input integer. For example, if my input number is 23. Then b[0] contains the BCD representation of 2 and b[1] contains the input representation of 3. I pass the individual digits 3 and 2 to the dectobin function and retrieve its BCD value which I am storing in array b.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    I am returning a 4 digit binary coded representation of a single integer from my function.
    Show an example of what you are trying to return from your function if the input parameter is equal to 3.

    Remember bcd[i] is not an array it is a single int.

    Jim

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    0101 is my return value if the input is 3. Thank you for your prompt reply.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Why are you using an array of int instead of a C-string to hold this information?

    First you need to actually get your function to work. To test your function try commenting out everything in main and just call your function. Your main would then look basically like:
    Code:
    int main(void)
    {
       dectobin(3);
    
       return 0;
    }
    Note: For now I am just going to ignore your return value.

    Run the program and see what happens. You may want to run this program through your debugger and put a break point on your return statement.

    Jim

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    I am using the below program to run as per your reply:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int *dectobin(int dig);
    void main(void)
    {
        dectobin(3);
        system("pause");
        return 0;
    }
    
    int *dectobin(int dig)
    {
        int r,i=0,j=0;
        int array[100]={0};
        static int reversearray[100]={0};
        while(dig!=0)
        {
        array[i]=dig%2;
        dig=dig/2;
        i=i+1;
        }
        for(;i>=0;i++)
        {
             reversearray[j]=array[i];
             j+=1;
        }
        return reversearray;
    }
    The debugger gives me the following information if I run the program:

    1. reversearray0<repeats 98 times>, 1992976512, 99 (putting the cursor on reversearray)
    2. return Not found in current context (putting the cursor on return statement)
    3. could not watch this variable (putting the cursor on dectobin)

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    And I also get a segmentation fault.


    Quote Originally Posted by jimblumberg View Post
    Why are you using an array of int instead of a C-string to hold this information?

    First you need to actually get your function to work. To test your function try commenting out everything in main and just call your function. Your main would then look basically like:
    Code:
    int main(void)
    {
       dectobin(3);
    
       return 0;
    }
    Note: For now I am just going to ignore your return value.

    Run the program and see what happens. You may want to run this program through your debugger and put a break point on your return statement.

    Jim

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    First main should be defined to return an int, int main(void).

    Did you place a break point on the return reversarray; line in your function? Did your program not seg fault on the following line in your function?
    Code:
            reversearray[j]=array[i];
    It did for me. The value of j was 600 and i was 602 when the program crashed.

    Look closely at your for loop, notice anything wrong? When will this loop ever end?


    Jim
    Last edited by jimblumberg; 04-08-2012 at 12:10 PM.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    I placed a breakpoint at the specified location, I am getting the following info on debugging:

    Code:
    reversearray0<repeats 100 times>at the point reversearray[j]=array[i];
    
    dectobin{int *(int)} 0x4012d9 <dectobin>
    Here's my modified code.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int *dectobin(int dig);
    int main(void)
    {
    dectobin(3);
    
           system("pause");
                   return 0;
    }
    int *dectobin(int dig)
    {
        int r,i=0,j=0;
        int array[100]={0};
        static int reversearray[100]={0};
        while(dig!=0)
        {
        array[i]=dig%2;
        dig=dig/2;
        i=i+1;
        }
        for(;i>=0;i--)
        {
             reversearray[j]=array[i];
             j+=1;
        }
        return reversearray;
    }

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Now since the program is not crashing try printing out the value of reversearray in your function before the return statement. Show the code for how you print out the values.

    Also if:
    I am returning a 4 digit binary coded representation of a single integer from my function.
    Why are you using arrays with a sizes of 100?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion from decimal to bianary
    By chaklader in forum C++ Programming
    Replies: 15
    Last Post: 01-29-2012, 11:35 AM
  2. Binary to decimal conversion
    By caysonmars in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 10:24 PM
  3. decimal to binary conversion
    By roelof in forum C Programming
    Replies: 41
    Last Post: 05-16-2011, 03:32 AM
  4. Decimal to Hex Conversion
    By millsy2000 in forum C Programming
    Replies: 7
    Last Post: 08-10-2010, 01:04 PM
  5. conversion of char to decimal
    By mapleleafblue in forum C Programming
    Replies: 4
    Last Post: 01-28-2009, 08:37 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21