Thread: binary to decimal ! how?

  1. #1
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Question 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "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

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  5. #5
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Angry to salem

    well that was not the answer I enquired Salem. thanku v m.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Post your best attempt... Then we'll help.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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 ).
    My best code is written with the delete key.

  9. #9
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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 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