Your main function will allow the user to choose between: 1) converting the bina
ID: 643525 • Letter: Y
Question
Your main function will allow the user to choose between:
1) converting the binary representation of a positive integer to the decimal representation of that number;
2) converting the decimal representation of a positive integer to the binary representation of that number; or
3) exiting the program.
*This menu should be in a loop so the user can convert as many numbers as they wish before exiting. When the user selects either of the first two options, your program will ask the user for a number to convert. It should first check to make sure the input is valid, then call a function that returns the desired value, and then print it for the user (the printing happens in main, not the conversion functions). Both conversion functions must be recursive.
Represent binary numbers as strings. Do not use any number base conversion functionality that is built into C++.
Checking whether a number is a valid integer can be done by reading the input into a string and then looping through it checking whether each character is a digit. For a binary number there are of course only two allowable characters. If the user enters invalid input, you should print a message to that effect. Then (instead of looping till they enter valid input) just return back to the main menu. Don't worry about clearing the screen to re-display the menu
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
bool checkDecimal(string value){
for(int i = 0; i < value.size(); ++i){
if(!(value[i] >= '0' && value[i] <= '9')){
if(i == 0 && value[i] == '+')
continue;
return false;
}
}
return true;
}
bool checkBinary(string value){
for(int i = 0; i < value.size(); ++i){
if(!(value[i] >= '0' && value[i] <= '1')){
return false;
}
}
return true;
}
int convertToDecimal(string value){
if(value.size() == 0){
return 0;
}
else{
string temp = "";
temp += value[0];
return (pow(2, value.size() - 1) * atoi(temp.c_str())) + convertToDecimal(value.substr(1));
}
}
string convertToBinary(int value){
if(value <= 1){
string temp = "";
temp += (char)(value + '0');
return temp;
}
else{
string temp = "";
temp += (char)((value % 2) + '0');
return convertToBinary(value / 2) + temp;
}
}
int main(){
int option;
string value;
while(true){
cout << "1. convert from binary to decimal" << endl;
cout << "2. convert from decimal to binary" << endl;
cout << "3. exit" << endl;
cout << "Plese choose an option: ";
cin >> option;
switch(option){
case 1:
cout << "Enter your binary Number: ";
cin >> value;
if(checkBinary(value)){
cout << "Decimal value is " << convertToDecimal(value) << endl;
}
else{
cout << "Input is invalid" << endl;
}
break;
case 2:
cout << "Enter your decimal Number: ";
cin >> value;
if(checkDecimal(value)){
cout << "Binary value is " << convertToBinary(atoi(value.c_str())) << endl;
}
else{
cout << "Input is invalid" << endl;
}
break;
case 3:
return 0;
break;
default:
cout << "Please choose a valid option" << endl;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.