Question

Create a c++ program which: 1. Displays a menu: 1) Decimal to Binary • 2) Binary...

Create a c++ program which:

1. Displays a menu: 1) Decimal to Binary • 2) Binary to Decimal • 3) Decimal to Hex • 4) Hex to Decimal • 9) Exit Program

2. For Menu item #1: Ask the user for a Decimal number. If they type a negative number, go back to step #1 1. Convert the Decimal number to binary and display it.

3. For Menu item #2: Ask the user for a binary number (1's and 0's). If they type something other than 1's and 0's, give them an error message and try again (step #1) 1. Convert the binary number to decimal and display it. 2. Note: Cap the length of the binary number to 8 bits. If they type more than that, give them an error message and go back to #1

4. For Menu item #3: Ask the user for a Decimal number. If they type a negative number, go back to step #1 1. Convert the Decimal number to hexadecimal and display it.

5. For Menu item #4: Ask the user for a hexadecimal number (0-9, a, b, c, d, e, f). If they type something other than any of those, give them an error message and try again (step #1) 1. Convert the hexadecimal number to decimal and display it. 2. Note: Cap the length of the hexadecimal digits 4 digits. If they type more than that, give them an error message and go back to #1

6. For Menu item #9, end the program

B. Notes: • Menu #1 should be a function called decimalToBinary • Menu #2 should be a function called binaryToDecimal • Menu #3 should be a function called decimalToHex Menu #2 should be a function called hexToDecimal

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

#include<iostream>
#include<sstream>
#include<cstring>
#include<bits/stdc++.h>

using namespace std;

void decimal_binary(int n)
{
   int arr[10];
   int i;  

    for(i=0; n>0; i++)  
   {
        //dividing by two and get the remainder stored in the array  
       arr[i]=n%2;  
       n= n/2;
   }  
   cout<<"Binary of the given number= ";
  
   //printing the binary number from the array
   for(i=i-1 ;i>=0 ;i--)  
   {  
       cout<<arr[i];  
   }
   cout<<"\n";
}

void binary_decimal(string bin)
{
   //creating object to convert string into int
   stringstream obj(bin);
  
   //getting the integer from the object
   int x=0;
   obj >> x;  
  
   //temporary variables
   int num = x;
     int decimal = 0;
   int t = num;
     int last;
   
    // base value to 1, that is 2^0
    int base = 1;
    
    while (t)
   {
   //getting the last digit
        last = t % 10;
      
        //dividing the given number by 10
        t = t / 10;

       // calculating the power of base for last digit
       // then the value is added to decimal
        decimal += last * base;

       //incrementing the base by 2
        base = base * 2;
    }
  
    //displaying the decimal value
    cout<<"Decimal equivaluent of the given binary= "<<decimal;
    cout<<"\n";
}

void decimal_hexa(int n)
{
   // array to store hexadecimal number
    char hexa[100];
    
    // counter for array
    int i = 0;
    while(n!=0)
    {  
        // variable to store remainder
        int temp = 0;
        
        // storing remainder in tempoarary variable.
        temp = n % 16;
        
        // check if temp < 10
        if(temp < 10)
        {
            hexa[i] = temp + 48;
            i++;
        }
        else
        {
            hexa[i] = temp + 55;
            i++;
        }
        
        n = n/16;
    }
    
    // printing array in reverse order
    cout<<"Hexadecimal equivalent of the given decimal= ";
    for(int j=i-1; j>=0; j--)
        cout << hexa[j];
   cout<<"\n";
}

void hexadecimal_binary(string hexVal)
{
   //finding the length of the string
    int len = hexVal.length();
  
    //converting the string to uppercase
    transform(hexVal.begin(), hexVal.end(), hexVal.begin(), ::toupper);
    
    // Initializing base value to 1, that is 16^0
    int base = 1;
    
    int dec_val = 0;
    
    // getting characters as digits from last character
    for (int i=len-1; i>=0; i--)
    {  
        // if character are '0'-'9', converted to number
        // 0-9 by subtracting 48 from ascii value.
        if (hexVal[i]>='0' && hexVal[i]<='9')
        {
            dec_val += (hexVal[i] - 48)*base;
                
            // incrementing base by power
            base = base * 16;
        }

       // if character are 'A'-'F', converted to number
        // 10-15 by subtracting 55 from ascii value.
      
        else if (hexVal[i]>='A' && hexVal[i]<='F')
        {
            dec_val += (hexVal[i] - 55)*base;
        
            // incrementing base by power
            base = base*16;
        }
    }
    //displaying the decimal value
    cout<<"Decimal equivalent of the given hexadecimal= "<< dec_val;
   cout<<"\n";
}
int main()
{
   int choice;
   int n;
   string bin,hexa;
  
   while(1)
   {
       cout<<"\nChoose any one of the following:"<<endl;
       cout<<"1.Decimal to Binary"<<endl;
       cout<<"2.Binary to Decimal"<<endl;
       cout<<"3.Decimal to Hexadecimal"<<endl;
       cout<<"4.Hexadecimal to Decimal"<<endl;
       cout<<"5.Exit"<<endl;
       cout<<"\n"<<endl;
       cout<<"Enter your choice: ";
       cin>>choice;
      
       switch(choice)
       {
           //for decimal to binary
           case 1: while(1)
                   {
                       //getting a number from the user
                       cout<<"Enter a decimal number: ";
                       cin>>n;
                  
                       //checking for valid input
                       if(n<0)  
                       {
                           cout<<"Error: Enter a positive numbers\n"<<endl;
                       }
                       else
                           break;
                      
                   }                  
                   //calling the function
                   decimal_binary(n);
                   break;
          
           //binary to decimal
           case 2: while(1)
                   {
                       int i=0;
                      
                       //getting the binary number from the user
                       cout<<"Enter a binary number: ";
                       cin>>bin;
                      
                       //falg variable
                       bool flag=true;
                  
                       //checking for valid binary digits
                       while (i < bin.length())
                          {
                          if( bin.at(i) != '1' && bin.at(i) != '0' )
                              {
                              flag = false;
                              }
                              i++;
                        }
                      
                        // printing the error message
                        if (flag==false)
                        {
                           cout<<"Error: please enter a valid binary number\n"<<endl;
                           continue;
                        }
                        else if(i>8)
                        {
                           cout<<"Error: Please binary digits of 8 only\n"<<endl;
                           continue;
                       }
                       else
                           break;  
                      
                   }
                   //calling the function
                   binary_decimal(bin);
                   break;          
          
           case 3:
                   while(1)
                   {
                       //getting decimal input from the user
                       cout<<"Enter a decimal number: ";
                       cin>>n;
                  
                       //checking for valid decimal input
                       if(n<0)  
                       {
                           cout<<"Error: Enter a positive numbers\n"<<endl;
                       }
                       else
                           break;                      
                   }
                   //calling the function
                   decimal_hexa(n);
                   break;          
          
           case 4: while(1)
                   {
                       int i=0;
                      
                       //getting the hexadecimal number from the user
                       cout<<"Enter a hexadecimal number: ";
                       cin>>hexa;
                      
                       //falg variable
                       bool flag=true;
                  
                       //checking for valid hexadecimal number
                       while (i < hexa.length())
                          {
                          if( !isxdigit(hexa[i]))
                        
                        
                              {
                              flag = false;
                              }
                              i++;
                        }
                      
                        //displaying the error message
                        if (flag==false)
                        {
                           cout<<"Error: please enter a valid hexadecimal number\n"<<endl;
                           continue;
                        }
                        else if(i>4)
                        {
                           cout<<"Error: Please hexadecimal digits of 4 only\n"<<endl;
                           continue;
                       }
                       else
                           break;                  
                      
                   }
                   //calling the function
                   hexadecimal_binary(hexa);
                   break;  
          
           case 5: exit(0);
          
           default: cout<<"Error: Enter a valid choice\n"<<endl;
              
       }
   }
  
   return 0;
}

SCREENSHOT FOR OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Create a c++ program which: 1. Displays a menu: 1) Decimal to Binary • 2) Binary...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • c programming Problem 2 Write a Hexadecimal to Decimal converter. Program will take an input (hexadecimal...

    c programming Problem 2 Write a Hexadecimal to Decimal converter. Program will take an input (hexadecimal number) from user and display its corresponding decimal number, as an output, on the screen. Moreover, after you're done with one conversion, ask for another input in an infinite loop fashion, link to online Hexadecimal to Decimal converter: https://www.binaryhexconverter.com/hex-to-decimal-converter 50 point

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • I am confused how to make program like convert binary to decimal and octal to decimal...

    I am confused how to make program like convert binary to decimal and octal to decimal using array in c++. This is question as below one. Your program must have at least two significant user defined functions, each using C++ arrays. Programs done using only main function, and done without using arrays, will get no credit. The program you will submit will have a menu driven system as below: Caution! No validation is done on numbers entered. They must conform...

  • Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects....

    Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- rectangle 2 -- circle 3 -- triangle 4 -- quit If the user selects choice 1, the program should find the area of a rectangle. rectangle area = length * width If the user selects choice 2, the program should find the area of a circle. circle area = PI * radius * radius...

  • Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....

    Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices. 1. Find the number of digits in an integer. 2. Find the nth digit in an integer. 3. Find the sum of all digits of an integer. 4. Is the integer a palindrome? 5. Quit Enter a choice: Page 1 of 4 For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • Write a C++ program that will display the following menu and work accordingly. A menu that...

    Write a C++ program that will display the following menu and work accordingly. A menu that will display the following choices and uses functions to carry out the calculations: 1. Call a function named classInfo() to ask the user the number of students and exams in a course. 2. Call a function getGrade() that will get the information from classInfo() and asks the user to enter the grades for each student. 3. Call a function courseGrade() to display the average...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT