1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Plz Help with this converter!

Discussion in 'C++' started by Mesum_naqvi, Mar 12, 2013.

  1. Mesum_naqvi

    Mesum_naqvi New 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;
    }
    
     
  2. Mesum_naqvi

    Mesum_naqvi New Member

    srry small correction to be made

    its member Shabbir
    forgive me:disappoin
     
  3. shabbir

    shabbir Administrator Staff Member

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

    Mesum_naqvi New 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
     
  5. shabbir

    shabbir Administrator Staff Member

    That just means you have to execute each once in a function and print the output.
     
  6. Mesum_naqvi

    Mesum_naqvi New 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 :nonod:
     
  7. shabbir

    shabbir Administrator Staff Member

    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.
     
  8. Mesum_naqvi

    Mesum_naqvi New Member

    i've tried everything :banghead: :banghead:

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

    Thank you
     
  9. shabbir

    shabbir Administrator Staff Member

    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.
     
  10. ManzZup

    ManzZup New Member

    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
     
  11. Mesum_naqvi

    Mesum_naqvi New Member

    k so i am just about giving up :cuss: with this program, but i am going to attempt one last try.
    i am getting a error in the following code.
    error C2660: 'PrintBinary' : function does not take 1 arguments
    error C2660: 'PrintOctal' : function does not take 1 arguments
    error C2660: 'PrintHex' : function does not take 1 arguments

    also i do need to make so changes to like adding my menu and other conversions but b4 i do that i just want to know if anyone point out if this it the correct way to go about??
    Thanx for all the help:pleased:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include "conio.h"
    using namespace std;
    
    void PrintOctal();
    void PrintBinary();
    void PrintHex();
    
    
    int main ()
    {
    	int iMyNumber;
    	cin >> iMyNumber;
    
    		PrintBinary(iMyNumber);
    		PrintOctal(iMyNumber);
    		PrintHex(iMyNumber);
    		cout << "Dec = " << iMyNumber <<endl;
    		cin.get();
    		return 0;
    	
    	return 0;
    }
    
    void PrintBinary(int iInteger)
    {
    	cout <<"Bin = ";
    	for (int i = 31; i >= 0; --i)
    	{
    		std::cout << ((iInteger >> i) % 2);
    	}
    
    	cout << " " <<endl;
    }
    
    void PrintOctal(int iInteger)
    {	
    	using namespace std;
    	cout << "Octal = ";
    	for (int i = 10; i >=0; --i)
    	{
    		int iOctDigit =((iInteger >> (3*i)) % 8);
    		cout << iOctDigit << "  ";
    	}
    	cout << endl;
    }
    
    void PrintHex(int iInteger)
    {
    	cout <<"Hex = ";
    	for (int i = 7; i >= 0; --i)
    	{
    		int iHexDigit = ((iInteger >>(4*i)) % 16);
    		if (iHexDigit < 10)
    		{
    			cout << "  " <<iHexDigit;
    		}
    		else
    		{
    			cout << "  " << (char) ('A' + (iHexDigit - 10));
    		}
    	}
    
     
  12. xpi0t0s

    xpi0t0s Mentor

    Not only what Shabbir said, but also your requirements seem to have changed:

    We are not going to write your code for you, but we can help you get unstuck. But this requires you to put some effort in.

    First you have to decide *exactly* what you want the program to do, because without that nobody stands a chance of getting anything right.

    Second you have to have a go at it yourself. You say you've tried everything, but there is no evidence of that. What is your latest attempt at the program, and where are you stuck? What is it doing that you didn't expect, or what isn't it doing that you did expect? What input did you give, what output did you get, and why is that output wrong? Have you identified the line of code where things start going wrong?

    Have you tried adding some debugging statements (printf is ideal for this) to display the values of relevant variables at key points in your code? I find this very useful for helping me debug a program that I can't get working. Better still if you can step through it in a debugger, then you can see exactly what it's doing and how that compares with what you THINK the code does.
     
    shabbir likes this.
  13. Mesum_naqvi

    Mesum_naqvi New Member

    Thank you for your reply and I don't know how else I can explain myself I never asked for anyone to write my program for me!!!. in my original post. I posted my attempt with the a problem. Then I was told to call the functions, so I tried that and posted what I tried and that didn't work so, Then I tried to do the program a different way using strings (which I don't know much) and I also stated that I need to make some changes I didn't add the menu and rest of the conversions because first I wanted to know if this would work.*Last but not least I clearly posted that I am completely new to programing I started last month*Well thanks again for your help*
     
  14. xpi0t0s

    xpi0t0s Mentor

    People often post homework assignments here expecting us just to do it for them.

    Take my questions one at a time. I have been programming since 1981 so I have a bit of a clue. You should be able to answer them all as they are not difficult, but I appreciate it can seem a bit much. If you answer my questions in as much detail as possible (just make your best effort) then we should be able to get this working properly together.
     
  15. Mesum_naqvi

    Mesum_naqvi New Member

    k so i going to post my code first which is a real mess and missing some code, then i will explain myself.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <conio.h>
    #include <math.h>
    #include <bitset>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    #define max 1000
    
    
    int menu();
    void Decimalnum();
    void Binarynum();
    void Octalnum();
    void Hexadecimalnum();
    
    void D2B();
    void D2O();
    void D2H();
    
    void B2D();
    void B2O();
    void B2H();
    
    void O2D();
    void O2B();
    void O2H();
    
    void H2D();
    void H2O();
    void H2B();
    
    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 binary number,base2"<<endl;
        cout <<"3-Enter a octal number,base2"<<endl;
        cout <<"4-Enter a hexadecimal number,base16"<<endl;
        cout <<" q Quit"<<endl <<endl;
        cout <<"Enter an option....";
        choice = _getch();
        cout <<endl <<endl;
    
    switch(choice)
        {
        case '1':
            Decimalnum();
            break;
        case '2':
            Binarynum();
            break;
        case '3':
            Octalnum();
            break;
        case '4':
            Hexadecimalnum();
            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;
        int D2B();
        int D2O();
        int D2H();
        system("pause");
        system("cls");
        return;
    }
    
    
    
    
    
    void Binarynum()
    {
        int num;
        cout <<"Option 2"<<endl;
    cout <<"This fucntion will convert a Binary number, base2 to hex base16, octal base8, and binary base10"<<endl<<endl;
        cout <<"Enter a Binary number,base10 :";
        cin >> num;
        cout <<"Base2  :";
        cout << num;
        int B2D();
        int B2O();
        int B2H();
        system("pause");
        system("cls");
        return;
    }
    void Octalnum()
    {
        int num;
        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 Octal number,base8 :";
        cin >> num;
        cout <<"Base8 :";
        cout << num;
        int O2D();
        int O2B();
        int O2H();
        system("pause");
        system("cls");
        return;
    }
    void Hexadecimalnum()
    {
        int num;
        cout <<"Option 4"<<endl;
    cout <<"This fucntion will convert a Hexadecimalnum number, base16 to decimal base10, octal base8, and binary base2"<<endl<<endl;
        cout <<"Enter a Hex number,base16 :";
        cin >> num;
        cout <<"Base16 :";
        cout << num;
        int H2D();
        int H2O();
        int H2D();
        system("pause");
        system("cls");
        return;
    }
    void D2B(){
       int n,bin[100],i,j;
        cout<<"Enter Base10 : ";
        cin >>n;
        cout<<"Base2 = ",n;
        for(i=0;n!=0;i++)
        {
            bin[i]=n%2;
            n=n/2;
        }
        for(j=i-1;j>=0;j--)
        {
            cout<<bin[j];
        }
        cout<<endl;
        system("pause");
        return;
    }
    void D2O(){
    int num,r[10],i;
        cout <<"Enter Base10 :";
        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 D2H(){
        int num,r[10],i;
        cout <<"Enter Base10:";
        cin >> num;
        cout <<"Base16 = ";
        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;
    }
    void B2D(){
        std::string binary2decimal_cstr;
        std::cout<<"Enter Base2 : ";
        std::cin>>binary2decimal_cstr;
        std::cout<<"Base10 = " <<std::bitset<32>(binary2decimal_cstr).to_ulong();
        std::cout<<std::endl;
        system("pause");
        return;
    }
    void B2O(){
            char num_str[50];
        cout<<"Enter Base2 : ";
        cin>>num_str;
        long num_1 = strtol(num_str,NULL,2);
        ltoa(num_1,num_str,8);
        cout<<"Base8 = " <<num_str<<endl<<endl;
        system("pause");
        return;
    }
    void B2H(){
        
     long int binary,hex=0,j=1,rem;
       printf("Enter Base2 : ");
       scanf("%ld",&binary);
       while(binary!=0){
       rem=binary%10;
       hex=hex+rem*j;
       j=j*2;
       binary=binary/10;
    }
     printf("Base16 : %lX \n",hex);
     system("pause");
     return;
    }
    void O2D(){
        int i,octnum,decnum=0,arr[20];
        cout <<"Enter Base8 : ";
        cin>>octnum;
        for(i=0;octnum>0;i++)
        {
          arr[i]=octnum%10;
          octnum=octnum/10;
          }
          cout<<i;
          for(int power=0,j=0;j<i;j++,power++)
          {
                  decnum = decnum + arr[j] * pow(8.0,power);
                  }
          cout <<"Base10 = "<<decnum;
          system("pause");
          return;
    }
    void O2B(){
        char oct[max];
        long int i=0;
        cout<<"Enter Base2: ";
        cin >>oct;
        cout <<"Base2 = ";
        while(oct[i]){
        switch(oct[i])
        {
         case '0': cout<<"000"; break;
         case '1': cout<<"001"; break;
         case '2': cout<<"010"; break;
         case '3': cout<<"011"; break;
         case '4': cout<<"100"; break;
         case '5': cout<<"101"; break;
         case '6': cout<<"110"; break;
         case '7': cout<<"111"; break;
         default:break;
                       }
        i++;
        }
    
        system("pause");
        return;
    }
    void O2H(){
    
    //I know in order to get hex octal number has to first convert into binary and then into hex but I cannot figure this out. 
    
    }
        
    void H2D(){
        int num;
        printf("Enter Base16 : ");
        scanf("%lX",&num);
        printf("Base2 = %ld \n",num);
        system("pause");
        return;
    }
    void H2O(){
        int num;
        printf("Enter Base16 : ");
        scanf("%lX",&num);
        printf("Base8 = %o \n",num);
        system("pause");
        return;
    }
    void H2B(){
        
    //I can't figure this one out as well I know it should be the opposite of binary to Hex it won't work .
    
    }
     
  16. Mesum_naqvi

    Mesum_naqvi New Member

    So as you see I have a lot of ---#include’s--- because I am not sure what should be included.
    First ---- I made 4 functions Decmalnum,Binarynum…….. and then tried to call the my conversion function in them like D2B(),D2O()…so on (which doesn’t work ) .
    Second ---- as you see I have some of the code in C instead of C++ it because most of my code is of the internet as I can’t figure it out myself. So if someone helps me convert it to C++ pls.
    Last ---- I am missing some code for which I understand the logic but can seem to put it together.
    I know I am going to hear a lot for this code but I can’t get help for anyone regarding this. :worried::worried:
    Thank you for any or all your help :nice::nice:
     
  17. xpi0t0s

    xpi0t0s Mentor

    C is a subset of C++ so all C code is also valid C++ code.

    You don't really need all 12 conversion functions. Just pick one as your "internal" format - decimal is probably easiest, then take the input in whatever format, convert it to the internal format, then display the three or four (depending on your requirements) formats. Then you will only need O2D, H2D, B2D, and D2O, D2H and D2B.

    In Decimalnum() you seem confused about how to call functions. If you prefix a function name with a type then that is a function definition, not a function call. Strangely in menu() you have the correct syntax for calling functions Decimalnum() etc.

    Focussing on Decimalnum(), you get the user to input the number they want to convert it and display it. You then call D2B, D2O and D2H, each of which asks the user to input their original number again. It'd be better to store that number in a variable, then pass that variable into those three functions.

    A comment on the amount of code you have written. Don't try to write the whole program in one go. It's not an essay, and the compiler does not only have to be called once. Start with a few lines of code (just your menu for example), compile it, if that works run it, and make sure that works OK. Then add a few more lines of code and make sure they work OK as well. Bit by bit build up your program. Otherwise you're going to repeat the same errors over and over again (like your wrong syntax for calling functions like D2B) and you'll get thousands of errors from the compiler and you won't have a clue where to start. If you only have a few lines of code that could go wrong then this limits the scope as to what the compiler will complain about.

    You don't need to set flag to zero in main, because of the return statement. If you have a return statement then the function returns at that point, not some time later. Do either "case 'q': flag=0; break;" or "case 'q': return 0;".
     
    shabbir likes this.
  18. Mesum_naqvi

    Mesum_naqvi New Member

    thank you, so is this what your talking about ??
    Code:
    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 binary number,base2"<<endl;
        cout <<"3-Enter a octal number,base8"<<endl;
        cout <<"4-Enter a hexadecimal number,base16"<<endl;
        cout <<" q Quit"<<endl <<endl;
        cout <<"Enter an option....";
        choice = _getch();
        cout <<endl <<endl;
    
    switch(choice)
        {
        case '1':
            Decimalnum();
            break;
        case '2':
            Binarynum();
            break;
        case '3':
            Octalnum();
            break;
        case '4':
            Hexadecimalnum();
            break;
        case 'q':
            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 <<endl;
        D2B(num);
        D2O(num);
        D2H(num);
        system("pause");
        system("cls");
        return;
    }
    void D2B(int n){
       int bin[100],i,j;
        
        cout<<"Base2 = ",n;
        for(i=0;n!=0;i++)
        {
            bin[i]=n%2;
            n=n/2;
        }
        for(j=i-1;j>=0;j--)
        {
            cout<<bin[j];
        }
        cout<<endl;
        return;
    }
    void  D2O(int num){
    int r[10],i;
        
        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;    
        return;
    }
    void D2H(int num){
        int r[10],i;
        
        cout <<"Base16 = ";
        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;
        return;
    }
    so now how do it reset of the conversions ?? because i think i still need one of each like B2D() , O2D() and H2D(), than i can re use the decimal function again but i get figure out how to do B2D() , O2D() and H2D() using integers i am using strings and i cant all my Decimal functions because of it. Help plz :wacky:
     
  19. xpi0t0s

    xpi0t0s Mentor

    Did you compile this code? Did you get any errors?
    If not, did it run OK and do what you expected?
    Did you think up any possible boundary conditions and test that the code worked there?

    Obviously the code as is will fail at compile time as Binarynum() and the other two are missing, but for now you could use stub code like:
    Code:
    void Binarynum()
    {
    cout << "Not written yet" << endl;
    }
    
    VERY BIG HINT FROM SOMEONE WHO HAS PROBABLY BEEN DOING THIS FOR LONGER THAN YOU HAVE BEEN ALIVE: do *not* proceed with anything else in this reply until you've done the above. I've already explained why.




    So, now you've got the above code working:

    B2D, O2D and H2D will basically be the reverse of the functions you've already coded. For example in binary, start with total=0, then if the rightmost bit is 1 then add 1; if the next bit is 1 then add 2; if the next bit is 1 then add 4, and for each subsequent bit add the next power of 2. (Don't use pow(); just double the column value on each iteration.)

    Oct and hex are a bit more complicated but not much: first add the rightmost digit of the octal number, then add the next digit multiplied by 8, then the next digit multiplied by 64 and so on. And for hex you will need to check if the digit is 0-9 or A-F and add 0-9 or 10-15 multiplied by the current column value (1, 16, 256 etc).

    If you use strings for these three functions then you can calculate the value of the digit very easily by just subtracting '0'. You don't need to know the ASCII codes. So '7'-'0'=7. For hex, for instance, 'c'-'a'=2, but c is 12 so add 10 to that: 'c'-'a'+10=12. You can use an int for the column value, and as you scan the string from right to left start with 1 then multiply it by 2, 8 or 16 depending on whether you're doing binary, oct or hex.
     
    shabbir likes this.
  20. Mesum_naqvi

    Mesum_naqvi New Member

    ok so far this code run and does exactly what i want:D:D:D, i am still working on the rest:crazy:

    Code:
    #include "stdafx.h"
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <conio.h>
    #include <math.h>
    #include <bitset>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    #define max 1000
    
    
    int menu();
    void Decimalnum();
    void Binarynum();
    void Octalnum();
    void Hexadecimalnum();
    
    void D2B(int);
    void D2O(int);
    void D2H(int);
    
    
    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 binary number,base2"<<endl;
        cout <<"3-Enter a octal number,base2"<<endl;
        cout <<"4-Enter a hexadecimal number,base16"<<endl;
        cout <<" q Quit"<<endl <<endl;
        cout <<"Enter an option....";
        choice = _getch();
        cout <<endl <<endl;
    
    switch(choice)
        {
        case '1':
            Decimalnum();
            break;
        case '2':
            Binarynum();
            break;
        case '3':
            Octalnum();
            break;
        case '4':
            Hexadecimalnum();
            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 <<endl;
        D2B(num);
        D2O(num);
        D2H(num);
        system("pause");
        system("cls");
        return;
    }
    void Binarynum()
    {
    	cout <<"Not written yet"<<endl;
        system("pause");
        system("cls");
        return;
    }
    void Octalnum()
    {
       cout <<"Not written yet"<<endl;
        system("pause");
        system("cls");
        return;
    }
    void Hexadecimalnum()
    {
        cout <<"Not written yet"<<endl;
        system("pause");
        system("cls");
        return;
    }
    void D2B(int num){
       int bin[100],i,j;
      
        cout<<"Base2 = ",num;
        for(i=0;num!=0;i++)
        {
            bin[i]=num%2;
            num=num/2;
        }
        for(j=i-1;j>=0;j--)
        {
            cout<<bin[j];
        }
        cout<<endl;
        return;
    }
    void D2O(int num){
    	int r[10],i;
       
        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;    
        return;
    }
    void D2H(int num){
        int r[10],i;
        
        cout <<"Base16 = ",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;
        return;
    }
     

Share This Page