Thread: Help with a decimal to binary converter

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Help with a decimal to binary converter

    Hi all,

    I would really appreciate help with the following code, Im very new to programming (2nd week actually!) and I cant seem to resolve a problem I am currently having! I would like to convert a decimal number to binary, but the output is (obviously) not correct for decimals that are not completely factorized for 2^x. For instance the decimal '4' will return the correct answer of "1-0-0" yet '6' will return the same number!? Ive poured over my code numerous times and there seems to be nothing wrong with my logic...
    Thanks a million to anyone that wouldnt mind helping a complete newbie!

    Code:
    // Daniel Erasmus
    // 0703393 W
    // Decimal --> Converter
    // 21/02/08 08:24
    
    # include <iostream>
    # include <math.h>
    
    using namespace std;
    
    int main ()
    {
        int decimal;
        int x;
    
        cout << "Welcome to the Decimal ----> Binary converter!" << endl << endl;
    
        cout << "Please enter decimal to be converted: ";
        cin >> decimal;
        cout << endl << endl;
    
        x = log (decimal+1) / log (2);
    
        for ( int y = pow (2, x) ; x >= 0 ; x--)
        {
            int m = decimal / y;
    
            if ( m >= 1 )
            {
                decimal = (decimal - y);
                cout << "1";
            }
            else if (m < 1 )
            {
                cout << "0";
            }
        }
    cin.get ();
    }
    On an unrelated note: I wonder if anyone could explain how to open a program from c++, ie: I have created a password pretection program and to try it out I would like a word document (in MS word) to open when the correct password is entered!!

    Thanks again!!!!
    Last edited by danielerasmus; 02-23-2008 at 03:08 AM. Reason: mistake

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're messing something up with your implicit conversions from double to int I believe, although your method of using log() only to use pow() seems somewhat convoluted.

    Quote Originally Posted by danielerasmus View Post
    On an unrelated note: I wonder if anyone could explain how to open a program from c++, ie: I have created a password pretection program and to try it out I would like a word document (in MS word) to open when the correct password is entered!!
    There are a number of ways. One is using system(). It's the most portable, but probably the least recommended. For Windows specifically, there are functions like ShellExecute() and ShellExecuteEx(). I believe the "most proper way" to actually start a process yourself in Windows is using CreateProcess().

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and of course to convert integer to binary using float fuctions like pow instead of simple bit-operations is a lot of overkill
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    thanks guys, but the debugger says the variables are at the correct values!! Im really not sure whats going wrong - and any and all help would be greatly appreciated!

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're not updating y every iteration.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    If you're working with bits you should use the bit operators. It's easier and faster.
    Code:
    #include <iostream>
    
    using namespace std;
    
    // Return a value with the bit specified
    // by i set and all other bits unset
    inline unsigned int bitmask ( int i )
    {
        return (1U << i);
    }
    
    int main()
    {
        // The number of bits in an int
        const int intSize = 31;
    
        // The decimal number to convert
        int value;
    
        cout << "Please enter a decimal number to be converted: ";
        cin >> value;
    
        // Print the bits of the decimal value
        for ( int i = intSize; i >= 0; --i )
        {
            // Assume bitmask( i ) is unset
            char bit = '0';
    
            // Pick the character to print
            if ( value & bitmask( i ) )
            {
                bit = '1';
            }
    
            cout << bit;
        }
    
        cout << endl;
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    thanks macgyver, i put the y updating expression inside the actual loop - thanks so so much! Im just not a hundread percent sure why it wasnt working before surely after each iteration of the loop the decleration of the variable is overwritten and updated??? :- )

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Review for loops. The initialization part of the loop is only done once at the start right before the first iteration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM
  3. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM