# Thread: binary to decimal ! how?

1. ## binary to decimal ! how?

Hello again fellow programmers.I just come across a problem.I want to convert a binary number to a decimal number by taking input from the user only once and giving him the answer.I have tried my best but can't execute the program.So can any one of u help me?

Use only if,else,if/else,for,do/while,while loops.

see ya. 2. We'll "see ya" when you've made an actual effort to try and solve the problem instead of just dumping your homework on the board!!!

> I have tried my best but can't execute the program
Good, so why not post it 3. 1. Get user input in form of string variable/object.
2. Initialize bitset object using above string variable.
3. Use to_ulong() method of the bitset to display a decimal representation of the user's input.

That's literally 3 lines of code minus all the #include's, the return 0; the open and closing braces, the variable declarations, and the int main() statement. 4. >That's literally 3 lines of code minus all the #include's, the return 0; the open and closing braces, the variable declarations, and the int main() statement.
A pity that this is probably an illegal solution. o0o's restrictions suggest a homework problem that requires a manual solution, much like the thousand other threads asking the same question in the past. 5. ## to salem

well that was not the answer I enquired Salem. thanku v m. 6. Post your best attempt... Then we'll help. 7. Ahhh... I think Prelude is right. You have to use only what you have studied so far. But, there is a big hint in hk_mp5kpdw's post too: Treat the input as a string, not as a number. Then, check each character in the string (in a loop) to see if it's a one or a zero and apply the appropriate weight for that position (1, 2, 4, 8...).

And, please read theboard guidelines about homework questions. It's OK to ask "How do I get started?", or "Here's what I have, but I can't get func(x) to work..." Try NOT to sound like you want someone to write the program for you. 8. >Treat the input as a string, not as a number.
This could be done just as easily with a number, but the length of the number would be restricted to the largest unsigned type allowed by C++ and leading 0's would cause a problem as they would be treated as octal values. But for simplicity of the implementation, I would say taking an unsigned long such as 1000001 and returning an unsigned int of the corresponding decimal (ie. 97) would be the best setup.

Using a string is more flexible, but not as easy to get right for valid cases. Of the three implementations I devised (unsigned long, std::string, C-string), all were less than ten lines, but the unsigned long solution was easier to follow at a glance than the other two. I would have to assume that a novice would have similar difficulties developing a string based solution than one using simple numeric operations (also assuming the novice in question knows how to gain access to the lowest digit with division and modulo ). 9. let's see...

what is a binary number?

1110001 = 1 * 2^0 + 0 * 2^1 + 0 * 2^2 + 0 * 2^3 ... etc

you see what to do now? Popular pages Recent additions 