Hi, I have a program problem with converting decimals to binary and back again. My teacher said that the program is to be like a menu and convert decimals or binary from the user's input. This is part of my code:
#include <iostream>
#include <string>
using namespace std;
void binary(int number) {
int remainder;
if(number <= 1)
cout << number;
remainder = number % 2;
binary(number >> 1);
cout << remainder;
}
int main(){
int number;
int choice;
cout << "1. Binary to Decimal\n";
cout << "2. Decimal to Binary\n";
cout << "3. Quit Program\n";
cout << "Enter your Choice: ";
cin >> choice;
switch (choice) {
case '1':
cout << "Please enter an integer: ";
cin >> number;
if (number < 0)
cout << "Error enter positive integer: ";
else {
cout << "The number in Binary is: ";
binary(number);
cout << endl;
}
}
system("pause");
return 0;
}
I know my problem is with the binary(int number) function because it is runnable but dosen't call to the function. So, my question is how can I modify this to run correctly? Also, how would I go about reversing the code to have it convert binary to decimal?