Hey guys I could really use some help here. In my program I need to ask the user for two 4 bit binary numbers . Then if they select add then add or if multiply then multiply. my problem is that I got the addition to work but the multiply is messing up somewheres. Or maybe I'm going about it the wrong way any ideas.
#include <iostream>
#include <string>
#include <fstream> //for ifstream,ofstream
#include <iomanip>
#include <stdio.h> // Need for getchar
using namespace std;
int main()
{
char num1[5];
char num2[5];
char multinum[5];
char multinum2[5];
char multinum3[5];
char multinum4[5];
char total[5];
int i;
bool done;
int j;
char option;
int carry;
carry = '0';
cout << " Enter 4 bit binay num" << endl;
cin >> num1;
cout << " Enter 4 bit binay num" << endl;
cin >> num2;
for ( i = 0; i <4 ; i++)
total[i] = '0';
for ( i = 0; i <4 ; i++)
multinum[i] = '0';
for ( i = 0; i <4 ; i++)
multinum2[i] = '0';
for ( i = 0; i <4 ; i++)
multinum3[i] = '0';
for ( i = 0; i <4 ; i++)
multinum4[i] = '0';
cout<< " What would you like to do? " << endl;
cout << " 1. Add \n" << "2. Multiply \n" << "3. Quit\n" << endl;
cin >> option;
/// add binary nums
if ( option == '1')
{
for ( i=3; i > -1; i--)
{
//cout << num1[i] << " " << num2[i] << " " << carry << endl ;
if ( num1[i]== '0' && num2[i] == '0' && carry == '0')
{
carry = '0';
total[i] = '0';
}
else if ( num1[i]=='1' && num2[i]=='1' && carry == '1' )
{
carry = '1';
total[i] = '1';
}
else if ( num1[i] == '1' && num2[i] == '1' && carry == '0' )
{
carry = '1';
total[i] = '0';
}
else if ( num1[i] == '1' && num2[i] == '0' && carry == '0' )
{
carry = '0' ;
total[i] = '1';
}
else if ( num1[i] == '1' && num2[i] == '0' && carry == '1' )
{
carry = '1';
total[i]= '0';
}
else if ( num1[i] == '0' && num2[i] == '0' && carry == '1' )
{
carry = '0' ;
total[i] = '1';
}
else if ( num1[i] == '0' && num2[i] == '1' && carry == '1' )
{
carry = '1' ;
total[i] = '0';
}
else if ( num1[i] == '0' && num2[i] == '1' && carry == '0' )
{
carry = '0' ;
total[i] = '1';
}
}
}
/// multiply binary nums
else if ( option == '2')
{
for ( i = 3 ; i > -1 ; i--)
{
if ( num2[i] == '1' && i == 3 )
strcpy(multinum,num1);
else if ( num2[i] == '0' && i == 3)
{
for ( j=3 ; j > -1; j--)
multinum[j] = '0';
}
else if ( num2[i] == '1' && i == 2)
{
multinum2[3] = '0';
multinum2[2] = num1[3];
multinum2[1] = num1[2];
multinum2[0] = num1[1];
}
else if ( num2[i] == '0' && i == 2 )
{
for ( j=3 ; j > -1; j--)
multinum2[j] = '0';
}
else if ( num2[i] == '1' && i == 1)
{
multinum3[3] = '0';
multinum3[2] = '0';
multinum3[1] = num1[3];
multinum3[0] = num1[2];
}
else if (num2[i] == '0' && i == 1)
{
for ( j=3 ; j > -1; j--)
multinum3[j] = '0';
}
else if (num2[i] == '1' && i == 0)
{
multinum4[3] = '0';
multinum4[2] = '0';
multinum4[1] = '0';
multinum4[0] = num1[3];
}
else if ( num2[i] == '0' && i == 0 )
{
for ( j=3 ; j > -1; j--)
multinum4[j] = '0';
}
}
}
for ( i = 3 ;i >-1; i--)
{
if (carry == '1' )
{
if ( multinum[i] == '0')
{
total[i] = '0';
carry = '0';
}
else if ( multinum[i] == '1')
{ carry = '0';
total[i] = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if (multinum[i] == '0' && multinum2[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '0')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '0')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '0')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '1' && multinum4[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '1')
{
total[i] = ' 0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '0' && multinum4[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '0')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '1' && multinum4[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '1' && multinum4[i] == '0')
{
total[i] = '1';
carry = '1';
}
}
if ( carry = '0')
{
if ( multinum[i] == '0')
{
total[i] = '0';
carry = '0';
}
else if ( multinum[i] == '1')
{ carry = '0';
total[i] = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if (multinum[i] == '0' && multinum2[i] == '1')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '0')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '0')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '1')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '0')
{
total[i] = '0';
carry = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '0')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '0')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '1' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '0';
carry = '0';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '1')
{
total[i] = '1';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '1' && multinum4[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '1')
{
total[i] = ' 1';
carry = '0';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '0' && multinum4[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '0' && multinum4[i] == '1')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '1' && multinum4[i] == '0')
{
total[i] = '0';
carry = '1';
}
else if ( multinum[i] == '0' && multinum2[i] == '1' && multinum3[i] == '0' && multinum4[i] == '0')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '0' && multinum2[i] == '0' && multinum3[i] == '1' && multinum4[i] == '0')
{
total[i] = '1';
carry = '0';
}
else if ( multinum[i] == '1' && multinum2[i] == '0' && multinum3[i] == '1' && multinum4[i] == '0')
{ total[i] = '0';
carry = '1';
}
}
}
//// print out for testing purpose
cout << "total" << endl;
for ( i=0; i < 4 ;i++)
cout << total[i];
cout << endl << " multinum ";
for ( i=0; i < 4 ;i++)
cout << multinum[i];
cout << endl << " multinum2 ";
for ( i=0; i < 4 ;i++)
cout << multinum2[i];
cout << endl << " multinum3 ";
for ( i=0; i < 4 ;i++)
cout << multinum3[i];
cout << endl << " multinum4 ";
for ( i=0; i < 4 ;i++)
cout << multinum4[i];
cout << endl << endl;
cout << " test" << endl;
cout.setf (ios::showpoint );
cout.setf( ios::fixed);
cout << setprecision(2);
cout << "\n Press enter to continue" << endl;
char ch = getchar();
return 0;
}