Thread: binary - decimal

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    binary - decimal

    This is an exercise from a book I am reading. It hasn't covered functions yet so without the use of functions the program is to take in an binary integer number and convert it the decimal equivalent.

    Here is my code.
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <cmath>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      int binary;
      int partial_binary;
      int decimal=0;
      int digit;
      int digit_count=0;
      
      cout << "Enter binary number:";
      cin >> binary;
      
      partial_binary=binary;
      
      while ((partial_binary / 10)!=0){
        digit=partial_binary % 10;
        partial_binary/=10;
        
        decimal+=(static_cast<int>(pow(2.0,digit_count))*digit);
        
        cout << "Digit number:"<<digit_count<<endl;
        cout << "Digit: "<<digit<<endl;
        cout << "Partial_binary:"<<partial_binary<<endl;
        cout << "Decimal:"<<decimal<<endl;
        
        digit_count++;
      
      
      }
      digit=partial_binary % 10;
      partial_binary/=10;
      decimal+=(static_cast<int>(pow(2.0,digit_count))*digit);
      
      cout << "Digit number:"<<digit_count<<endl;
      cout << "Digit: "<<digit<<endl;
      cout << "Partial_binary:"<<partial_binary<<endl;
      cout << "Decimal:"<<decimal<<endl;
        
      cout << "The binary number "<<binary<<" equals "<<decimal<<" in decimal."<<endl;
      
      system("PAUSE");	
      return 0;
    }
    My question is concerning my while loop is there some way I can have it execute one last time so I can eliminate the last digit, partial_binary, decimal calculation outside the loop?

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I didn't really study the code... so I'm gussing... but it sounds like you want to change from a while-loop to a do-while-loop.

    A while-loop tests the condition at the begining, and the do-while loop tests the condition at the end. (A do-while loop will always execute at least once.)

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    The problem isn't in entering the loop the first time. It is in executing one last time past the test condition in the while loop.
    It does work, it's just seems a little redundant coding the last calculations.


    PS I did try a do while loop the first time. What needs to be changed is my test condition somehow. Just thought someone would have better ideas. Using a string to input the binary number would of course be better but that wasn't the problem description given in the book. Also I should probably include some error checking to test for valid input, that I can do simply by checking each digit to see if it is 0 or 1. Most of the output in my code is simply for my own debugging purposes.
    Last edited by curlious; 07-31-2003 at 05:48 AM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Does changing your loop control to this help:
    >>while (partial_binary != 0)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just as a taste of what awaits you in the future when you get ready to tackle the STL, the same basic thing you are doing can be accomplished in a few simple steps using a bitset container:
    Code:
    #include <iostream>
    #include <bitset>
    #include <limits>
    #include <string>
    
    int main()
    {
        std::string data;
    
        std::cout << "Enter in a binary value to be converted to decimal: ";
        std::cin >> data;
    
        std::bitset<std::numeric_limits<long>::digits> bits(data);
    
        std::cout << "The converted decimal value is: " << bits.to_ulong() << std::endl;
    
        return 0;
    }
    Sample output:
    Enter in a binary value to be converted to decimal: 1101101
    The converted decimal value is: 109

    In the meantime, keep pluggin away at what you are doing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    nice very nice I can hardly wait thanks for the input

    Thanks hammer guess a person can't always see the obvious considering I was already dividing partial_binary by 10 in my loop!
    Last edited by curlious; 07-31-2003 at 02:28 PM.

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. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. 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
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 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