# Thread: Help with a decimal to binary converter

1. ## Help with a decimal to binary converter

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!

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 ();
}```
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!!

Thanks again!!!!

2. You're messing something up with your implicit conversions from double to int I believe, although your method of using log() only to use pow() seems somewhat convoluted.

Originally Posted by danielerasmus
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!!
There are a number of ways. One is using system(). It's the most portable, but probably the least recommended. For Windows specifically, there are functions like ShellExecute() and ShellExecuteEx(). I believe the "most proper way" to actually start a process yourself in Windows is using CreateProcess().

3. and of course to convert integer to binary using float fuctions like pow instead of simple bit-operations is a lot of overkill

4. thanks guys, but the debugger says the variables are at the correct values!! Im really not sure whats going wrong - and any and all help would be greatly appreciated!

5. You're not updating y every iteration.

6. If you're working with bits you should use the bit operators. It's easier and faster.
Code:
```#include <iostream>

using namespace std;

// Return a value with the bit specified
// by i set and all other bits unset
inline unsigned int bitmask ( int i )
{
return (1U << i);
}

int main()
{
// The number of bits in an int
const int intSize = 31;

// The decimal number to convert
int value;

cout << "Please enter a decimal number to be converted: ";
cin >> value;

// Print the bits of the decimal value
for ( int i = intSize; i >= 0; --i )
{
// Assume bitmask( i ) is unset
char bit = '0';

// Pick the character to print
if ( value & bitmask( i ) )
{
bit = '1';
}

cout << bit;
}

cout << endl;

return 0;
}```

7. thanks macgyver, i put the y updating expression inside the actual loop - thanks so so much! Im just not a hundread percent sure why it wasnt working before surely after each iteration of the loop the decleration of the variable is overwritten and updated??? :- )

8. Review for loops. The initialization part of the loop is only done once at the start right before the first iteration.