Trouble With Binary Converter

I made a converter that takes a decimal number and converts it to binary but when I get to the conversion part the numbers dont print? Can somebody please help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include <iostream>

void convert (int number, int x);

int main()
{
	using namespace std;
	cout << "Please enter a number to be converted to binary: ";
	int number;
	cin >> number;
	int bits;
	if (number <= 15)
	{
		bits = 4;
	}
	else if (number <= 255)
	{
		bits = 8;
	}
	else if (number <= 4095)
	{
		bits = 12;
	}
	else if (number <= 65535)
	{
		bits = 16;
	}
	int largestnumber;
	if (bits == 4)
	{
		largestnumber = 8;
	}
	else if (bits == 8)
	{
		largestnumber = 128;
	}
	else if (bits == 12)
	{
		largestnumber = 2048;
	}
	else if (bits == 16)
	{
		largestnumber = 32768;
	}
	cout << "The number in binary is: ";
	convert (number, largestnumber);
	return 0;
}

void convert (int number, int x)
{
	using namespace std;
	for (x; x == 0; x = x/2)
	{
		if (number >= x)
		{
		cout << "1";
		number -= x;
		}
		else
		{
			cout << "0";
		}
	}
}
for (x; x == 0; x = x/2)

the first part of a for loop initialises... that is ok.. it is already initialised
the last part is the change every round... also ok.
but the middle one needs to be true as long as you want to loop to continue...
and x is not zero when you start, so the loop will never execute...

and further more...

using namespace std;
place it once above main under includes.. not inside functions...

And maybe you can rethink your whole idea.
it is a lot of code for a simple task..

tip:
use bit shifting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string bitVal = "";
   int a = 0xAAAA;
   while( a != 0 )
  {
     bitVal  = (a & 1 ) ? "1" + bitVal  : "0" + bitVal ;
     a = a >> 1;
  }
  cout << bitVal;

  /* pause */
  getchar();
  return 0;
}
The code works but it puts the binary backwards!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	cout<<"Please enter a number to be converted to binary: ";
	int number;
	cin>>number;
	string bitVal = "";
	while (number != 0)
	{
		if (number & 1)
		{
			bitVal += "1";
		}
		else
		{
			bitVal += "0";
		}
		number = number >> 1;

	}
	cout << bitVal;
	return 0;
}
I fixed the backwards issue by reversing the string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void rev_str(string str,int n);

int main()
{
	cout<<"Please enter a number to be converted to binary: ";
	int number;
	cin>>number;
	string bitVal = "";
	while (number != 0)
	{
		if (number & 1)
		{
			bitVal += "1";
		}
		else
		{
			bitVal += "0";
		}
		number = number >> 1;

	}
	bitVal = string(bitVal.rbegin(), bitVal.rend());
	cout<<"The binary number is: "<<bitVal<<endl;
	return 0;
}
You can still optimized and smarter in code
for example

main()
{
	int decimal, reminder, i = 1, binary = 0;
	cout< <"Enter the decimal to be converted:"<<endl;
	cin>>decimal;
	do
	{
		reminder = decimal % 2;
		binary = binary +  (i * reminder);
		decimal = decimal / 2;
		i= i * 10;
	}while(decimal > 0);
	cout< <"The binary of the given number is:"<<binary<<endl;
	getch();
}
Last edited on
I fixed the backwards issue by reversing the string.


this is like writing CBA and copying you work to ABC..
it is to much work...

just to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
while (number != 0)
	{
		if (number & 1)
		{
			bitVal = "1" + bitVal ;
		}
		else
		{
			bitVal = "0" + bitVal ;
		}
		number = number >> 1;

	}


and btw that is exaclty the same as what i had;)
1
2
3
4
5
while( a != 0 )
  {
     bitVal  = (a & 1 ) ? "1" + bitVal  : "0" + bitVal ;
     a = a >> 1;
  }


You can still optimized and smarter in code

smarter code?

the % operator is slow compared to & operator...
just use decimal & 1 it is much faster...
the same counts for / operator.. use decimal >> 1; instead...
and why do you use int to store the binary?
it has a max lenght compared to a string...
Last edited on
Topic archived. No new replies allowed.