complete will result in a failing grade for the class A. Create a Windows based
ID: 3741788 • Letter: C
Question
complete will result in a failing grade for the class A. Create a Windows based c++ program which: Asks the user for a binary number (1's and O's). If they type something other tha 1. n 1 s and 0's, give them an error message andtry again (step #1) 2. (Convert the binary number to decimal and display fit. Asks the user for a decimal number. If they type something other than a decimal number, give them an error message and try again (step #3) Convert that number to binary and display it. Keep asking until the user types QUIT or quit in #1 #2 should be a function called binaryTDecimal #4 should be a function called decima!TOBinary A menu would be delicious (as shown in the example output section of this document) The Binary to Decimal and Decimal to Binary functions should be just that, 3. 4. 5. 6, 7. 8. 9. functions ssumptions Here are some of the things I assume you know how to do attending class and reading the text(s). You understand how to write, compile and test a C++ program using Eclipse. Vouunderstand how to convert from binary to decimal and decimal to binary as weExplanation / Answer
here you go buddy
// code below
#include <iostream>
#include <cmath>
#include<string>
using namespace std;
int convertBinaryToDecimal(long long);
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
long long convertDecimalToBinary(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
int main()
{
long long n;
string str;
while( true){
if (str == "quit"){
break;}
cout << "Enter a 1 to convert decimal to binary or 2 for binary to decimal ";
cout<<"enter quit to quit ";
getline(cin,str);
if (str == "1"){
cout << "Enter a decimal number: ";
cin >> n;
cout << convertDecimalToBinary(n);
return 0;
}
if (str == "2"){
cout << "Enter a binary number: ";
cin >> n;
cout<< convertBinaryToDecimal(n)<< endl ;
return 0;}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.