+ Reply to Thread
Results 1 to 17 of 17
  1. #1
    Junior Member
    Join Date
    Jul 2003
    Posts
    25

    Converting binary to decimal

     
    I need to convert binary (base-2) numbers to decimal (base-10) numbers using C++. And I have to do it one digit at a time, left to right, using the cin.get function.

    I'm having trouble with this.

    Any help?

    Thanks.

  2. #2
    Senior Member
    Join Date
    Oct 2001
    Location
    Utah
    Posts
    551
    from left to right?
    it'd be easier the other way,
    But just start doing some pseudo code for your algorithm.

    I suggest reading all the values into an array, Because you have to know how many.
    Then just go along the array(backwards starting at lowest digit) and multiply the number by 2 to the x where x is the position.

    So...

    read in numbers, init array..

    for loop i from 1 to length of array
    total = total + array[i] * 2^i
    end loop

    you'll need to play with the loop, make sure it's going the right length, multiplying the right stuff, etc..

    good luck..
    dragonb

  3. #3
    Junior Member
    Join Date
    Jul 2003
    Posts
    25
    Sorry, I'm a total novice with this stuff.

    I don't think I know how to create an array.

    I also need to give an "invalid" message when a non-binary number is entered. What command do I use to make sure it's binary?

    I mean:

    cout << "Enter binary number: ";

    while (number != binaryBase2)
    cout << "Invalid, try again";

    What can I put in place of "binaryBase2" to make it work? And if the user has to try again, how do I make it jump back up to the "enter binary number"?

  4. #4
    Member
    Join Date
    Apr 2002
    Location
    Georgia
    Posts
    137
    Well, why don't you first off set a limit as to maximum number of bits that the largest number that can be read in can be.

    Then, you find out how many bits are in the number.

    Then, if there is a 1 in that bit position, add in 2 raised to the power of that bit position to your running total.

    Do this from left to right, stripping off each bit as you do so.

    The answer should be the decimal equivalent of that binary number. (Check it by using the Calculator on your computer to see if you did this right)

    Also, a binary number only has 0's and 1's. Any number with digits other than 0's and 1's isn't binary. That should be enough information for you to test if it is binary or not.
    Jüš† ä €öm¶ù†Ê® §ÇÌÈñŒ mÅjÒ®

  5. #5
    Junior Member
    Join Date
    Jul 2003
    Posts
    25
    Thanks for the help guys!

  6. #6
    Junior Member
    Join Date
    Oct 2005
    Posts
    4
    i just got the same assignment, and am VERY stuck. Does anyone wanna code this up using the cin.get function and reading the values from left to right? Damn hard assignment if u ask me for being in the 5th week of an intro computer science class.

  7. #7
    Member
    Join Date
    Sep 2002
    Posts
    364
    How do you expect to learn if you ask others to do your assignments?

  8. #8
    Real gangstas sip on Yacc jkrohn's Avatar
    Join Date
    Oct 2001
    Location
    Suckas-ville
    Posts
    4,554
    Osmosis. After we write it out, he will print it and strap it onto his head with one of those 80's headbands.

    Jkrohn
    Signatures blow hard
    If your signature contains an ad of any kind, congratulations, you're on my ignore list.

  9. #9
    Junior Member
    Join Date
    Oct 2005
    Posts
    4
    i learn by example. I can't figure out the logic behind it, and if i can't even get passed that step, there is no way to figure out to code it.

  10. #10
    Junior Member
    Join Date
    Oct 2005
    Posts
    4
    my prof says to convert it "on the fly", and i don't even know what she means by that

  11. #11
    Real gangstas sip on Yacc jkrohn's Avatar
    Join Date
    Oct 2001
    Location
    Suckas-ville
    Posts
    4,554
    Ok, if you need to do it on the fly, then data entered as such:
    110101010 would really be 010101011 as a binary number.
    It is actually pretty easy. I am tired and don't want to look up all the syntax
    Code:
    int input = i = running_total = 0;
    do{
    cout<<"Enter the next digit.  Enter -1 to stop";
    cin>>input;
    running_total += (input * pow(2,i));
    i++;
    }while(input != -1);
    cout<<"Total: "<<running_total;
    Or something similar to that. If you need to keep the number, put it into a string and reverse it.

    Jkrohn
    Signatures blow hard
    If your signature contains an ad of any kind, congratulations, you're on my ignore list.

  12. #12
    Junior Member
    Join Date
    Oct 2005
    Posts
    4
    the way you have it, it asks for each digit individually? What we need is to do it on the fly, from left to right, and all at once.

  13. #13
    Real gangstas sip on Yacc jkrohn's Avatar
    Join Date
    Oct 2001
    Location
    Suckas-ville
    Posts
    4,554
    If you can't tell that the above code is asking for each one and doing it on the fly, you are in quite a bit of trouble.

    Jkrohn
    Signatures blow hard
    If your signature contains an ad of any kind, congratulations, you're on my ignore list.

  14. #14
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,327
    Another case of poor teachers making a simple assignment hard.

    Ask the teach for PRECISE instructions. I do it all the time with my users; if they can't give me EXACT specifications of what they want, I won't code it for them (you learn this after redoing a program 20 or 30 times due to inaccurate or missing specifications).

    You new guys have got to learn to start doing algorithms on paper or a whiteboard or something before you start chomping at the keys or you're going to have a lot of frustration. You'll be pleasantly surprised that when you can write out or see a problem done on paper, how easy it becomes to transfer it to code (in any language)

  15. #15
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,327
    JK, with regards to my comment about specs...

    Your code is fine, but that is ASSUMING the teach wants the first digit entered as 2^n vs. 2 ^0

  16. #16
    Real gangstas sip on Yacc jkrohn's Avatar
    Join Date
    Oct 2001
    Location
    Suckas-ville
    Posts
    4,554
    That code I posted takes the input string it assuimg you are staring with 2^0 up to 2^n. It can't be done the other way (starting with 2^n) unless you know the number of digits you are getting ahead of time.

    That is the only way to do it without taking in the whole string and dumping into a string and then reversing it. Which IMO is not "on the fly".

    Jkrohn
    Signatures blow hard
    If your signature contains an ad of any kind, congratulations, you're on my ignore list.

  17. #17
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,327
    Yeah, you're right....my bad.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews