Thread: Need some advice

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    1

    Need some advice

    i have this code to convert dinary to decimal but when i enter a binary number all it does is return the number of digits i typed i.e. if i typed 110010 it should return 50 but I get 6. Any help/advice would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*function prototype*/
    int bin2dec(int binar4y);
    
    int main()
    {
        int binary;
        int decimal;
        
    
         printf("The decimal equivalent = %d\n",bin2dec(binary));
            
    getchar();
    getchar();
    return 0;
    }
    
    int bin2dec(int binary)
    {
             
             int b=0;
             int num[10];
             int i=0;
             int t=0;
             
             printf("Enter Binary Number\n");
             scanf("%d", &binary);
             while(binary != 0)
             {
              num[i] = binary % 10;
              binary = binary / 10;
              if(num[i] != 1 && num[i] != 0)
               t = 1;
              i++;
             }
             num[i] = 2;
             binary = 1;
             for(i = 0; num[i] != 2; i++)
             {
              b = b + num[i] * binary;
              binary = binary * 2;
             }
            
    }
    Last edited by Dave_Sinkula; 09-23-2006 at 09:33 AM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps bin2dec should return a value.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  3. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 01:06 PM
  4. need hardware advice....
    By forgottenPWord in forum Tech Board
    Replies: 5
    Last Post: 03-23-2003, 09:12 AM
  5. Newbie: Seek advice
    By gogo in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 10:04 AM