Hi all,
I would really appreciate help with the following code, Im very new to programming (2nd week actually!) and I cant seem to resolve a problem I am currently having! I would like to convert a decimal number to binary, but the output is (obviously) not correct for decimals that are not completely factorized for 2^x. For instance the decimal '4' will return the correct answer of "1-0-0" yet '6' will return the same number!? Ive poured over my code numerous times and there seems to be nothing wrong with my logic...
Thanks a million to anyone that wouldnt mind helping a complete newbie!
On an unrelated note: I wonder if anyone could explain how to open a program from c++, ie: I have created a password pretection program and to try it out I would like a word document (in MS word) to open when the correct password is entered!!Code:// Daniel Erasmus // 0703393 W // Decimal --> Converter // 21/02/08 08:24 # include <iostream> # include <math.h> using namespace std; int main () { int decimal; int x; cout << "Welcome to the Decimal ----> Binary converter!" << endl << endl; cout << "Please enter decimal to be converted: "; cin >> decimal; cout << endl << endl; x = log (decimal+1) / log (2); for ( int y = pow (2, x) ; x >= 0 ; x--) { int m = decimal / y; if ( m >= 1 ) { decimal = (decimal - y); cout << "1"; } else if (m < 1 ) { cout << "0"; } } cin.get (); }
Thanks again!!!!