Question

In: Computer Science

Make a simple C++ program not complex Any number is entered from keyboard of base 2,...

Make a simple C++ program not complex

  1. Any number is entered from keyboard of base 2, base 8, base 10 or base 16.
  2. Program asks for conversion options (menu) to convert into required base, i.e., 2, 8, 10 or 16.
  3. User selects the option and program converts the input number to the selected base number.
  4. Output number is shown on the screen.

Following points are to be considered while making the program:

  • It should be a menu driven program, use switch/case for menu. Program must have following options:
    Select Input Number Base
    1 for Binary
    2 for Octal
    3 for Decimal
    4 for Hexadecimal
    5 for Exit

    When user selects option from 1 to 5, User inputs the number.
    After number is entered, program displays following menu:
    1 for conversion to Binary
    2 for conversion to Octal
    3 for conversion to Decimal
    4 for converesion to Hexadecimal
    5 for Exit

    After user input, the program converts the input number to the given base, and displays the result.
  • After number display, pressing any key will again takes back to the first menu for input number.
  • Program should run until and unless user presses EXIT.

Solutions

Expert Solution

Step 1 : Save the following code in Conversion.cpp

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

int main() {
int fromBase;
int toBase;
int input;
string binary;
while (true) {
// Display menu
cout << "Select Input Number Base" << endl;
cout << "1 for Binary" << endl;
cout << "2 for Octal" << endl;
cout << "3 for Decimal" << endl;
cout << "4 for Hexadecimal" << endl;
cout << "5 for Exit" << endl;
cin >> fromBase;
if (5 == fromBase) {
return 0;
}
cout << "Enter the number : ";
// For binary read string and use stoi() to convert to integer.
// For others, use keyword like oct, dec hex etc
switch (fromBase) {
case 1 : cin >> binary; // string binary = "100100101"   
input = stoi(binary, 0, 2);
break;
case 2 : cin >> oct >> input;
break;
case 3 : cin >> input;
break;
case 4 : cin >> hex >> input;
break;
default : cout << "Invalid choice";
break;
}
cout << "Select Output Number Base" << endl;
cout << "1 for conversion to Binary" << endl;
cout << "2 for conversion to Octal" << endl;
cout << "3 for conversion to Decimal" << endl;
cout << "4 for conversion to Hexadecimal" << endl;
cout << "5 for Exit" << endl;
cin >> toBase;
if (5 == toBase) {
return 0;
}
cout << "Output : ";
// For binary use for loop to extract every digit.
// For others, use keyword like oct, dec hex etc
switch (toBase) {
case 1: for (int i = 7; i >= 0; i--) {
cout << ((input >> i) & 1);
}
cout << endl;
break;
case 2: cout << oct << input << endl;
break;
case 3: cout << dec << input << endl;
break;
case 4: cout << hex << input << endl;
break;
default: cout << "Invalid choice";
break;
}
}
  
return 0;
}

Step 2 : Compile the program using g++ or visual studio and run.

Following is my sample output


Related Solutions

c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
C ++ code that accepts any measurement in meters entered by the user through the keyboard...
C ++ code that accepts any measurement in meters entered by the user through the keyboard and displays the equivalent measurement in feet on the console (1 Meters = 3.2808 Feet). Use proper variable names.
2. Create a C++ program that converts the number of American dollars entered by the user...
2. Create a C++ program that converts the number of American dollars entered by the user into one of the following foreign currencies: Euro, British pound, German mark, or Swiss franc. Allow the user to select the foreign currency from a menu. Store the exchange rates in a four-element double array named rates. Notice that the menu choice is always one number more than the subscript of its corresponding rate. For example, menu choice 1's rate is stored in the...
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system...
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system to base-16 number system Convert 100 from base-10 number system to base-2 number system Convert 100 from base-10 number system to base-16 number system Convert ef from base-16 number system to base-2 number system Convert ef from base-16 number system to base-10 number system
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered through the keyboard.(method) (Factorial of n: n! = 1*2*3*…*n, 0! = 1.) 2. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (method) Please post a screenshot of the codes. Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT