#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    1
    Rep Power
    0

    Binary to Decimal Code, needing help.


    After a rather long break from programming in C I recently started again. After brusing up on it I decided to write this code off the top of my head.

    I'm not quite sure exactly what's wrong with it, I've spent a few hours just staring at it. Theoretically, when run it prompts you to enter the binary to be converted. (currently only 4 digits for simplicity's sake.) and then return a value after the calculations have been made. In reality, At a DOS prompt it asks for the Binary and after that has been entered it encounters a fatal error.

    I've broken up the code into sevral functions after writing it in one big Chunk. If you can debug this simple program, please please e-mail me with the code and a short explanation of what was wrong. My E-mail is thewiccakid@hotmail.com , thank you.
    -JG

    PHP Code:
    #include <stdio.h>
    #include <string.h>
    int buns[4];
    int weiner[4];
    int a 0;
    int yz;

    void calculate_1(){
    for (
    04y++){
        
    weiner[y] = buns[y]*2^(y);
    }}

    int calculate_2(){
    for (
    04y++) {
        
    weiner[y] = z;
       
    += z;
    }
    return 
    0;
    }

    int results(){
    printf("The decimal equivalent of %d is:\n"buns);
    printf("%d"a);
    return 
    0;
    }

    main ()
    {
    printf("Please input the Binary to be converted to decimal:\n");
    scanf("%d %d %d %d"buns[0], buns[1], buns[2], buns[3] );
    calculate_1();
    calculate_2();
    results();

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    56
    Rep Power
    11
    First off, move your variables into main and then pass and return your arrays or data. Also, put a type on main. void main or int main with a return 0 at the end of main will suffice.

    Second, your problem I believe lies in calculate_2:

    weiner[y] = z;

    should be switched to read.

    z = weiner[y];

    but why don't you just do

    a += weiner[y];

    Third, the ^ isn't doing what you want it to do. It's actually a bitwise xor operation.

    Here's some code I dug up that does it for any length of number entered. Only problem is that you still reach the limit of an int (32bit or so) on the input. Since you're brushing up on C again it's always good to look at different methods, etc.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	int count, total, i, j, tmp;
    	char buffer[256];
    
    	total = 0;
    
    	printf("Please enter a number: ");
    	scanf("%s", buffer);
    	count = strlen(buffer);
    
    	for (i = 0; i <= count; i++)
    	{
    		if (buffer[count-i] == '1')
    		{
    			tmp = 1;
    			for (j = 1; j < i; j++)
    				tmp *= 2;
    			total += tmp;
    		}
    	}
    
    	printf("\nNumber is: %d\n", total);
    	
    	return 0;
    }
    Last edited by helado; July 6th, 2003 at 12:02 AM.

IMN logo majestic logo threadwatch logo seochat tools logo