Converting Binary to Decimal using while loop in C++ -


i reading book on c++ , there task asks reader convert binary number (inputted user) decimal equivalent. far have following code, output 0. idea has gone wrong?

#include <iostream> using namespace std;  int main() { int x, b = 10, decimalvalue = 0, d, e, f, count = 1, counter = 0; cout << "enter binary number: "; cin >> x; x /= 10; while( x > 0) { count++; x = x/10; } while (counter < count) {       if (x == 1)       {             f = 1;       }       else{            f = x % b;       }       if (f != 0)       {             if (counter == 0)             {                decimalvalue = 1;             }             else             {                e = 1;                d = 1;                while (d <= counter)                {                   e *= 2;                   d++;                }                decimalvalue += e;             }       }       x /= b;       counter++; } cout << decimalvalue << endl; system("pause"); return 0; 

}

because while( x > 0) loop stops when x <= 0. besides, cin >> x lets user input decimal number.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -