
binary  decimal
This is an exercise from a book I am reading. It hasn't covered functions yet so without the use of functions the program is to take in an binary integer number and convert it the decimal equivalent.
Here is my code.
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cmath>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
int binary;
int partial_binary;
int decimal=0;
int digit;
int digit_count=0;
cout << "Enter binary number:";
cin >> binary;
partial_binary=binary;
while ((partial_binary / 10)!=0){
digit=partial_binary % 10;
partial_binary/=10;
decimal+=(static_cast<int>(pow(2.0,digit_count))*digit);
cout << "Digit number:"<<digit_count<<endl;
cout << "Digit: "<<digit<<endl;
cout << "Partial_binary:"<<partial_binary<<endl;
cout << "Decimal:"<<decimal<<endl;
digit_count++;
}
digit=partial_binary % 10;
partial_binary/=10;
decimal+=(static_cast<int>(pow(2.0,digit_count))*digit);
cout << "Digit number:"<<digit_count<<endl;
cout << "Digit: "<<digit<<endl;
cout << "Partial_binary:"<<partial_binary<<endl;
cout << "Decimal:"<<decimal<<endl;
cout << "The binary number "<<binary<<" equals "<<decimal<<" in decimal."<<endl;
system("PAUSE");
return 0;
}
My question is concerning my while loop is there some way I can have it execute one last time so I can eliminate the last digit, partial_binary, decimal calculation outside the loop?

I didn't really study the code... so I'm gussing... but it sounds like you want to change from a whileloop to a dowhileloop.
A whileloop tests the condition at the begining, and the dowhile loop tests the condition at the end. (A dowhile loop will always execute at least once.)

The problem isn't in entering the loop the first time. It is in executing one last time past the test condition in the while loop.
It does work, it's just seems a little redundant coding the last calculations.
PS I did try a do while loop the first time. What needs to be changed is my test condition somehow. Just thought someone would have better ideas. Using a string to input the binary number would of course be better but that wasn't the problem description given in the book. Also I should probably include some error checking to test for valid input, that I can do simply by checking each digit to see if it is 0 or 1. Most of the output in my code is simply for my own debugging purposes.

Does changing your loop control to this help:
>>while (partial_binary != 0)

Just as a taste of what awaits you in the future when you get ready to tackle the STL, the same basic thing you are doing can be accomplished in a few simple steps using a bitset container:
Code:
#include <iostream>
#include <bitset>
#include <limits>
#include <string>
int main()
{
std::string data;
std::cout << "Enter in a binary value to be converted to decimal: ";
std::cin >> data;
std::bitset<std::numeric_limits<long>::digits> bits(data);
std::cout << "The converted decimal value is: " << bits.to_ulong() << std::endl;
return 0;
}
Sample output:
Enter in a binary value to be converted to decimal: 1101101
The converted decimal value is: 109
In the meantime, keep pluggin away at what you are doing.

nice very nice I can hardly wait thanks for the input :)
Thanks hammer guess a person can't always see the obvious considering I was already dividing partial_binary by 10 in my loop!