Thread: looping

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

    looping

    Trying to make a program with binary numbers that turn into decimals. i got the first digit, but am stuck trying to isolate to second digit to turn into dec. any help?







    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    unsigned number;
    unsigned count= 0;
    
    
    
    cout<<"Please enter in a binary number, and I will give you the number ";
    cout<< "in decimal form"<<endl;
    cin>>number;
    while (number=number%10)
    {
     if (number == 0)
       count = count + 0;
        if(number==1)
                count = count + 1;
    break;
    }
    
    while(number=number%100)
    {
      if (number == 0)
         count = count + 0;
      if (number == 1)
         count = count +2;
         break;
    }
    
    cout<<count;
    
    
     system("pause");
     return 0;   
        
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Did you really need a second thread for your question that we already answered ?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Those loops don't look anything like what I suggested in the previous thread. I suggest you break the validation (making sure user input integer consists only of 1's or 0's) and the conversion (taking integer and converting to decimal) into individual functions. Both of these would have the same basic loop structure as in the other post.

    The validation function can be something that returns a bool (true/false) value and accepts an integer as an argument. Check each digit using the same kind of loop as previously shown to you. If you reach something that is not a 1 or a 0, you can immediately return false. Otherwise, if you make it to the end of the loop/function you can return true.

    I've already made a suggestion on how to do the conversion in the other thread; just wrap that in a handy function and your program would be 95% done.
    "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. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM