Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Plz Help with this converter! (http://www.go4expert.com/forums/plz-help-converter-t29540/)

Mesum_naqvi 12Mar2013 21:26

Plz Help with this converter!
 
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;
}


Mesum_naqvi 12Mar2013 21:33

Re: Plz Help with this converter!
 
srry small correction to be made

its member Shabbir
forgive me:disappoin

shabbir 12Mar2013 21:40

Re: Plz Help with this converter!
 
Quote:

Originally Posted by Mesum_naqvi (Post 99215)
srry small correction to be made

its member Shabbir
forgive me:disappoin

Perfectly fine but also share what is wrong in the above code as it becomes easier for us to look and suggests

Mesum_naqvi 12Mar2013 21:58

Re: Plz Help with this converter!
 
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

shabbir 12Mar2013 23:33

Re: Plz Help with this converter!
 
That just means you have to execute each once in a function and print the output.

Mesum_naqvi 13Mar2013 00:56

Re: Plz Help with this converter!
 
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 :nonod:

shabbir 13Mar2013 09:43

Re: Plz Help with this converter!
 
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.

Mesum_naqvi 18Mar2013 04:10

Re: Plz Help with this converter!
 
i've tried everything :banghead: :banghead:

anyone else want to take a shot at it :confused::confused:

Thank you

shabbir 18Mar2013 09:55

Re: Plz Help with this converter!
 
Quote:

Originally Posted by Mesum_naqvi (Post 99253)
i've tried everything :banghead: :banghead:

anyone else want to take a shot at it :confused::confused:

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.

ManzZup 21Mar2013 13:19

Re: Plz Help with this converter!
 
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


All times are GMT +5.5. The time now is 10:04.