for (bool keepOn = true; keepOn; ) {
cout << endl << "\t\tBinary Math" << endl << endl;
...the rest of your code as you have it
}
and adding a case 5
case 5:
keepOn = false;
break;
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
/* ADDITION CODE */
string DtoB (int Decimal); // Convert Decimal to Binary
void BaseGen(int Base[], int Size); // load the base value of binary number
int BtoD(string Binary); // convert Binary to decimal
string AddBin(string Str1, string Str2); // add two binary number
string Str1, Str2; // Declare Str1 and Str2
string Sum ; // Sum will hold the returned total
string DtoB (int Decimal)
{
string Bin ="00000000"; // declare a model binary number
int Base[8];
BaseGen(Base,8);// to load the base values
int Index=7;
while (Index>=0)
{
if (Decimal>=Base[Index])
{
Decimal=Decimal-Base[Index];
Bin[Bin.size()-1-Index]='1';
}
Index=Index-1;// reduce the Index
}
return Bin;
}
void BaseGen(int Base[], int Size)
{
Base[0]=1; // the first element is always 1
int Index=1; // start with the second value
while(Index<Size) // must load all Base elements
{
Base[Index]=Base[Index-1]*2; // left = right *2
Index=Index+1; // Bump the Index
}
return ;
}
int BtoD(string Binary)
{
unsigned int Index=1; //loop control variable
Index=0; // Initialize to zero
int Dv=0; //Dv is the decimal value for the binary number in Binary
int Base[8]; // Base array to store binary base values
BaseGen(Base,8); // Load Base
while(Index<Binary.size()) // loop to check all bits
{
if (Binary[Index]=='1') // if the value is 1
Dv=Dv+Base[Binary.size()-1-Index]; // add the corresponding base value to Dv
Index=Index+1; // Bump the loop control variable
}
return Dv; // Return the decimal value
}
string AddBin(string Str1, string Str2)
{
string Sum="00000000";
int Int1,Int2, Isum; // DELCARE INTERGERS TO HOLD CONVERTED VALUE
Int1=BtoD(Str1); // convert Str1 to decimal
Int2=BtoD(Str2); // convert Str2 to decimal
Isum=Int1+Int2; // Calculate the sum of two integer
Sum=DtoB(Isum); // convert Isum to binary
return Sum; // return to caller
}
/* END ADDITION CODE */
int main()
{
int Choice;
cout << endl << "\t\tBinary Math" << endl << endl;
cout << "1. Find the Sum of Two Binary Numbers " << endl;
cout << "2. Find the Difference of Two Binary Numbers" << endl;
cout << "3. Find the Product of Two Binary Numbers" << endl;
cout << "4. Find the Quotient of Two Binary Numbers" << endl;
cout << "5. Exit" << endl << endl;
cout << "Enter number 1 - 5 for your Choice: ";
cin >> Choice;
switch(Choice)
{
case 1:
cout << "\n" << endl;
cout << "Enter Two Binary Numbers With a Sum Less Than 255" << "\n" << endl;
cout << "First Number: ";
cin >> Str1;
cout << "Second Number: ";
cin >> Str2;
Sum = AddBin(Str1, Str2);
cout << "Sum: " << Sum << endl;
cout << "\n" << endl;
system("PAUSE");
break;
case 2:
cout << "2" << endl;
system("PAUSE");
break;
case 3:
cout << "3" << endl;
system("PAUSE");
break;
case 4:
cout << "4" << endl;
system("PAUSE");
break;
default:
return(0);
break;
}
}
int BtoD( string Str )
{
int rc = 0;
int Index=0;
while( Index < Str.size()
{
rc = rc * 2;
if( Str[Index] != '0' )
{
rc = rc + 1;
}
Index = Index - 1;
}
return rc;
}
string DtoB( int Value )
{
string rc = "00000000";
int Index=8;
while( Index > 0 )
{
Index = Index - 1;
int Value2 = Value / 2;
int Value3 = Value2 * 2;
if( Value != Value2 ) //then the right most digit was NOT a zero
{
rc[Index] = '1';
}
Value = Value2;
}
}
string DtoB_AlternatePlan( int Value )
{
string rc = "00000000";
int Index=8;
while( Index > 0 )
{
Index = Index - 1;
if( Value & 1 ) //then the right most digit was NOT a zero
{
rc[Index] = '1';
}
Value = Value / 2;
}
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
How should I use Artificial Intelligence to sort out relevant statements from non-relevant ones? | 4 | 109 | |
Is AWS.RDS considered a part of "Big Data"? | 4 | 93 | |
Question to Pivot table | 1 | 33 | |
Interview questions for support of a legacy ASP.NET site | 4 | 37 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
14 Experts available now in Live!