Problems with binary long handling

  c++

I made a binary decimal conversion menu which always take 0 for binary numbers

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    
    int dec,ch,i;
    long bin,temp;
    do
    {
        dec=bin=i=ch=0;
        cout<<"nnttMENUn1. Deciml to Binary numbern2. Binary to Decimal numbern3. Exitn";
        cout<<"Enter your choice(1/2/3)> ";
        cin>>ch;
        switch(ch)
        {
            case 1: cout<<"Enter a decimal number: ";cin>>dec;
                temp=dec;
                while(dec)
                {
                    bin+=(dec%2)*pow(10,i);
                    dec/=2;
                    i++;
                }
                cout<<temp<<" in decimal = "<<bin<<" in binary"<<endl;break;
            case 2: cout<<"Enter a binary number: ";cin>>bin;
                temp=bin;
                while(bin)
                {
                    dec+=(bin%10)*pow(2,i);
                    bin/=10;
                    i++;
                }
                cout<<temp<<" in binary = "<<dec<<" in decimal"<<endl;break;
            case 3: break;
            default:
                cout<<"Invalid choice";
        }
    }while(ch!=3);
}

When I select choice 1 and enter 23, it gives 0 and when I enter 11001 in choice 2 it gives 0 and tells that I entered 0.

Output:



                MENU
1. Deciml to Binary number
2. Binary to Decimal number
3. Exit
Enter your choice(1/2/3)> 1
Enter a decimal number: 23
23 in decimal = 0 in binary


                MENU
1. Deciml to Binary number
2. Binary to Decimal number
3. Exit
Enter your choice(1/2/3)> 2
Enter a binary number: 11001
0 in binary = 0 in decimal


                MENU
1. Deciml to Binary number
2. Binary to Decimal number
3. Exit
Enter your choice(1/2/3)> 3

--------------------------------
Process exited after 7.103 seconds with return value 0
Press any key to continue . . .

Source: Windows Questions C++

LEAVE A COMMENT