Plz Help with this converter!

Mesum_naqvi's Avatar, Join Date: Mar 2013
Go4Expert Member
Hey everyone can someone plz help or guide me in the right direction.
i have to make a converter that will convert (Binary,Decimal,Octal,and Hex)
1-input base10-----output base2,base8, and base16.
2-input base2-------output base10,base8,and base16.
3-input base8-------output base2,base10, and base16.
4-input base16------output base2,base10,and base8.

i was able to write Decimal to Binary,Octal,and Hex with the Help of member Sabbir , like to his code that i used as guide.

my code that works but its completely wrong

Code:
int menu();
void Decimalnum();
void Hexadecimalnum();
void Octalnum();
void Binarynum();


int main()
{
	cout <<"Lab-4 Binary,Decimal,Octal,and Hex converter"<<endl;

	menu();
	return 0;
}
int menu()
{
	char choice;
	int flag = 1;
	while (flag ==1)
	{
	cout <<"Choose the base value option intput"<<endl;
	cout <<"1-Enter a decimal number,base10"<<endl;
	cout <<"2-Enter a hexadecimal number,base16"<<endl;
	cout <<"3-Enter a octal number,base2"<<endl;
	cout <<"4-Enter a binary number,base2"<<endl;
	cout <<" q Quit"<<endl <<endl;
	cout <<"Enter an option....";
	choice = _getch();
	cout <<endl <<endl;

switch(choice)
	{
	case '1':
		Decimalnum();
		break;
	case '2':
		Hexadecimalnum();
		break;
	case '3':
		Octalnum();
		break;
	case '4':
		Binarynum();
		break;
	case 'q': flag = 0;
		return 0;
	 }
  }
}
void Decimalnum()
{
	int num;
	cout <<"Option 1"<<endl;
	cout <<"This fucntion will convert a decimal number, base10 to hex base16, octal base8, and binary base2"<<endl<<endl;
	cout <<"Enter a decimal number,base10 :";
	cin >> num;
	cout <<"Base10 :";
	cout << num;
	system("pause");
	system("cls");
	return;
}
void Binarynum()
{
	int num,bin[100],i,j;
	cout <<"Base2:";
	cin >> num;
	for (i=0;num!=0;i++)
	{
		bin[i]= num%2;
		num=num/2;
	}
	for (j=i-1;j>=0;j--)
	{
		cout <<bin[j];
		return;
	}
}
void Octalnum()
{
	int num,r[10],i;
	cout <<"Option 3"<<endl;
	cout <<"This fucntion will convert a Octal number, base8 to hex base16, decimal base10, and binary base2"<<endl<<endl;
	cout <<"Enter a number to find it's octal :";
	cin >> num;
	cout <<"Base8 :",num;
	for (i=0;num!=0;i++)
	{
		r[i]=num%8;
		num = num/8;
	}
	i--;
	for(;i>=0;i--)
	cout <<r[i];
	cout <<endl;    
	system("pause");
	system("cls");
	return;
}
void Hexadecimalnum()
{
	int num,r[10],i;
	cout <<"Option 2"<<endl;
	cout <<"This fucntion will convert a hexadecimal number, base16 to decimal base10, octal base8, and binary base2"<<endl<<endl;
	cout <<"Base16:";
	cin >> num;
	for (i=0;num!=0;i++)
	{
		r[i]=num%16;
		num=num/16;
	}
	i--;
	for (;i>=0;i--)
	{
		if(r[i]==10)
            cout << "A";
        else if(r[i]==11)
            cout <<"B";
        else if(r[i]==12)
            cout <<"C";
        else if(r[i]==13)
            cout <<"D";
        else if(r[i]==14)
            cout <<"E";
        else if(r[i]==15)
            cout <<"F";
        else
            cout <<r[i];
    }
    cout <<endl;
	system("pause");
	system("cls");
	return;
}
0
Mesum_naqvi's Avatar, Join Date: Mar 2013
Go4Expert Member
srry small correction to be made

its member Shabbir
forgive me
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Mesum_naqvi View Post
srry small correction to be made

its member Shabbir
forgive me
Perfectly fine but also share what is wrong in the above code as it becomes easier for us to look and suggests
0
Mesum_naqvi's Avatar, Join Date: Mar 2013
Go4Expert Member
the code works fine but it only converts.
Decimal to binary
Decimal to Octal
Decimal to Hex
in that order and one at a time depending on user input

but what i want to do is
user input ---- base10(decimal #) example = 44
i want out output as :
base10=44
base2=00101100
base8=54
base16=2c

everthing as the same time
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
That just means you have to execute each once in a function and print the output.
0
Mesum_naqvi's Avatar, Join Date: Mar 2013
Go4Expert Member
i am new to programming so sorry for being slow

so your saying that i do this:

void Decimal_to_rest()
{
_____________
then jst call
decimal_to_binary();
decimal_to_octal();
decimal_to_hex();
}

if so then i have tried that it doesn't let me do that
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Those functions defined asks for user input and so they must be changed such that it passes a parameter which is converted and printed. Input should be outside of those functions.
0
Mesum_naqvi's Avatar, Join Date: Mar 2013
Go4Expert Member
i've tried everything

anyone else want to take a shot at it

Thank you
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Mesum_naqvi View Post
i've tried everything

anyone else want to take a shot at it

Thank you
If you don't show what you have tried, it would be tough for anyone to help you. What you are looking for is pretty much the basics.
0
ManzZup's Avatar, Join Date: May 2009
Skilled contributor
i'm not sure but i assume that you can always find a std method or at least a pre written code that works fine
i mean if this is not for leaning purpose
you can ise itoa