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.
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?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; }