why cannot convert more than 10 binary digits to decimal number

hhave downloaded this program that convert binary numbers to decimal number. But, I found that it works properly with binary numbers upto 10 digits. It gives wrong results with 11 and more binary digits.

would any one help me to develope the program or at least tell me how to fix this problem.

the program as shown below:

//--------------------this program allow user to convert binary to decimal and Decimal to binary --------------
//-------------------- tested unber C++ Builder 6 (Borland)-----------------------
//-------------------- created by holy_spirit ------------------------------------

#include <iostream>
//#include <conio>
#include <cmath>

using namespace std;
// three function prototype

int menuBar () ;
void binToDec ();
void decToBin ();

void main ()
{

while ( 1 )
{
// clear screen it belong to ( #include <conio.h> )
// clrscr ();

int choice = menuBar ();

switch ( choice )
{
// when user select convet binary to decimal
case 1 :
//clrscr ();

binToDec ();

cout << "\npress any key to return to main menu : \n";
cout<<endl;

break;

// when user select convert decimal to binary
case 2 :
//clrscr ();

decToBin ();

cout << "\n\n\npress any key to return to main menu : \n";
//getch ();

break;
case 3 :
exit ( 0 );


default :
//clrscr () ;
cout << "\nplease select from list below : \n" << endl
<< "\npress any key to continue\n" ;
//getch ();
}
}
}

int menuBar ( )
{
int choice ;

cout << "\n1- convert Binary to Decimal \n" << endl
<< "\n2- convert Decimal to Binary \n" << endl
<< "\n3- exit \n" << endl ;
// get the choice fro user
cin >> choice ;

return choice ;
}

// convert decimal to binary
void decToBin ()
{
int num ;

int res [100] ;

int count = -1 ;

cout << "\nenter your Decimal number : \n";
cin >> num ;

while ( num != 0)
{
count ++ ;
res [count] = num % 2;
num /= 2 ;
}

for ( count ; count >= 0; count-- )
cout << res [count];
}


// convert binary to decimal
void binToDec ()
{
int num ;
int res = 0;

cout << "\nenter your Binary number : \n";
cin >> num ;

for ( int i = 0; num > 0; i++)
{
res += (num % 10) *( pow ( 2,i )) ;

num -= num % 10;
num /= 10;
}

cout << "\nYour Decimal number is : \n" << res << endl;
}
See, the problem is that whoever wrote this was a total noob at the time.
He made several mistakes, like getting user input from a conversion function, using ints to store binary digits that will be converted to decimal, and using pow().

A 32-bit signed int can hold any integer in the range [-2^31;2^31-1]. 2^31 happens to have 10 decimal digits.
Now, the problem is that, since this is a binary number that will be converted to decimal, the minimum number of bits the user should be able to input is the size of a long (in this case, 32 bits). There is no integer large enough to hold that, so the bits should be stored as a string.

That program is made of fail. I could a better one with my eyes closed.
As a matter of fact,
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
#include <iostream>
#include <string>

unsigned bin2dec(std::string &bin){
	unsigned res=0;
	for (unsigned a=0;a<bin.size();a++){
		if (bin[a]!='0' && bin[a]!='1')
			continue;
		res<<=1;
		res|=bin[a]-'0';
	}
	return res;
}

std::string *dec2bin(unsigned dec){
	std::string *res=new std::string;
	bool leadingzeroes=0;
	unsigned bits=sizeof(unsigned)*8;
	unsigned loops=bits;
	while (loops>0){
		loops--;
		bool digit=dec>>(bits-1);
		dec<<=1;
		if (!digit && !leadingzeroes)
			continue;
		leadingzeroes=1;
		res->push_back(digit+'0');
	}
	return res;
}

int main(){
	std::string bin;
	std::cout <<"Input a binary number (up to "<<(sizeof(unsigned)*8)<<" bits): ";
	std::getline(std::cin,bin);
	std::cout <<"In decimal: "<<bin2dec(bin)<<std::endl;
	unsigned dec;
	std::cout <<"Input a decimal number: ";
	std::cin >>dec;
	std::string *bin2=dec2bin(dec);
	std::cout <<"In binary: "<<*bin2<<std::endl;
	delete bin2;
	return 0;
}
Topic archived. No new replies allowed.