# looping

This is a discussion on looping within the C++ Programming forums, part of the General Programming Boards category; Trying to make a program with binary numbers that turn into decimals. i got the first digit, but am stuck ...

1. ## 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. Did you really need a second thread for your question that we already answered ?

3. 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.

Popular pages Recent additions