Thread: Help with digits

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Help with digits

    Write a program that reads in an unsigned integer containing only 0s and 1s (i.e. a "binary" integer) and outputs its decimal equivalent. Use the modulus and division operators to pick off the "binary" number's digits one at a time from rigiht to left. Much as in the decimal number system, where the rightmost digit has a positional value of 1 (=100), the next digit left has a positional value of 10 (=101), then 100 (=102), then 1000 (=103), and so on, in the binary number system the rightmost digit has a positional value of 1 (=20), the next digit left has a positional value of 2 (=21), then 4 (=22), then 8 (=23), and so on. Thus, the decimal number 234 can be interpreted as 2*100 + 3*10 + 4*1.

    Additional requirements:

    Your program must accept only (unsigned) "binary"integers containing only 0s and 1s. Do not read in the integer as a string object. Use a loop to validate the input: the user should get stuck entering a "binary" integer if the value entered is not of the right form.

    I don't understand how to start the program off using the modulus and division operators to seperate the number into digits. Does anybody have any suggestions? I just started programming and this is my first assignment.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int value = 12345;
    
    while( value > 0 )
    {
        cout << value &#37; 10 << endl;
        value /= 10;
    }
    Should output:
    Code:
    5
    4
    3
    2
    1
    "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

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    I have it to where I can enter my int.

    What kind of statement would I use to, take that digit, and to check to see if it is <2, then give it a power to turn it into decimal form.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    What kind of statement would I use to, take that digit, and to check to see if it is <2, then give it a power to turn it into decimal form.
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    int value = 12345;
    
    while( value > 0 )
    {
        cout << value % 10 << endl;
        value /= 10;
    }
    Should output:
    Code:
    5
    4
    3
    2
    1
    *cough*

    Did you even read the first reply ?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need something to hold a sum and a counter initialized to 0. In the loop, once you've got the particular digit you are dealing with in the current iteration of the loop, you add to the sum an amount equal to the current digit (which will always be either a 1 or a 0) times 2 raised to the power which is currently stored in the counter. You can use the pow function for that. Towards the end of the loop, you can increment counter by 1.

    To do the validation, just loop through and check if each digit is either a 0 or a 1.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 04:05 PM
  5. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 10:39 PM