Something strange

I tried to make a program to convert a long int in a binary number.
This is the program:

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
long int x;
short int b = 1;
short int y[50];

cout << "Enter an integer: ";
cin >>x;
cout << "\n";

do
{
float a , d;

a = x/2;
long int c = a;
d = c;

if(a - d == 0)
{
y[b] = 0;
}

else
{
y[b] = 1;
}

x = c;
b++;

}while(x != 0);

for(short int b = b - 1 ; b >= 1 ; b--)
{
cout << y[b];
}
getch();

return 0;

}

It gives something very strange, what's wrong with it?
You have a couple of coding errors, but I think the overall algorithm is flawed.

You are attempting to use truncation from floating point to integer to determine if x is evenly divisible by 2. But x % 2 does the same thing without requiring floating point.

Your for loop at the bottom declares b as a short int which is shadowing the declaration of b as a short int at the top of main.
Topic archived. No new replies allowed.