Thread: Converting binary to decimal
-
September 30th, 2003, 03:23 AM #1Junior 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.
-
September 30th, 2003, 12:33 PM #2Senior 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
-
October 1st, 2003, 01:14 AM #3Junior 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"?
-
October 1st, 2003, 02:25 AM #4Member
- 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Ò®
-
October 5th, 2003, 04:01 AM #5Junior Member
- Join Date
- Jul 2003
- Posts
- 25
Thanks for the help guys!
-
October 10th, 2005, 06:23 PM #6Junior 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.
-
October 10th, 2005, 08:22 PM #7Member
- Join Date
- Sep 2002
- Posts
- 364
How do you expect to learn if you ask others to do your assignments?
-
October 10th, 2005, 08:26 PM #8
Osmosis. After we write it out, he will print it and strap it onto his head with one of those 80's headbands.
JkrohnSignatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
-
October 11th, 2005, 01:29 AM #9Junior 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.
-
October 11th, 2005, 01:32 AM #10Junior 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
-
October 11th, 2005, 02:32 AM #11
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
Or something similar to that. If you need to keep the number, put it into a string and reverse it.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;
JkrohnSignatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
-
October 11th, 2005, 02:34 AM #12Junior 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.
-
October 12th, 2005, 09:10 AM #13
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.
JkrohnSignatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
-
October 12th, 2005, 01:22 PM #14
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)
-
October 12th, 2005, 01:27 PM #15
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
-
October 12th, 2005, 09:51 PM #16
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".
JkrohnSignatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
-
October 13th, 2005, 07:14 AM #17
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)



LinkBack URL
About LinkBacks



Reply With Quote



Watch Fast and Furious 6 online Free here and you won’t see a faster, more furious movie this year. Gina Carano joins the ensemble as Luke Hobbs (Dwayne Johnson)’s new partner. They enlist the help...
Watch Fast and Furious 6 online...