I am having problems with my program to add and multiply two 4 bit binary numbers. The part in which I am currently having trouble is when I run the program the output for the addition function is not correct. Instead of displaying the correct answer it is just displaying 3 3 3 3. Any help would be greatly appreciated.
Main:
#include "binNum.h"
#include "misc_ops.h"
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
int i = 0;
binNum num;
num.numbers();
num.addition();
num.convert(i);
//keeps the output window open
cout << "Press enter to continue \n";
cin.sync();
cin.ignore();
char ch = getchar();
return 0;
}
binNum.h
#ifndef BINNUM_H
#define BINNUM_H
using namespace std;
class binNum
{
private:
char num [5], num2 [5], c [5], sum [5];
int i, j, k, x, carry;
char result;
public:
binNum(); //Default constructor
void numbers();
void addition();
void multiplication();
void convert(int);
~binNum();
};
#endif
binNum.cpp
#include "binNum.h" //obtain prototype definitions
#include <iostream>
using namespace std;
binNum::binNum()
{
i = 0;
carry = 0;
k = 0;
x = 0;
result = ' ';
}
void binNum::numbers()
{
cout << "Enter a 4 bit binary number entering only 0's and 1's: ";
for ( i=0; i < 4; i++)
{
cin >> num[i];
break;
}
//cin.get(num[i]);
cout << "Enter another 4 bit binary number entering only 0's and 1's: ";
for (i = 0; i < 4; i++)
{
cin >> num2[i];
//break;
}
//cin.get(num2[i]);
}
void binNum::addition()
{
for(i = 3; i <= 0 ; i--)
{
if (num[i] == '1' && num2[i] == '1' && carry == '1')
{
c[j] = 1;
carry = 1;
sum[k]=1;
result = sum[k];
}
else if (num[i] == 1 && num2[i] == 1 && carry == 0)
{
c[j] = 0;
carry = 1;
sum[k] = 0;
result = sum[k];
}
else if (num[i] == 0 && num2[i] == 1 && carry == 0)
{
c[j] = 1;
carry = 0;
sum[k] = 1;
result = sum[k];
}
else if (num[i] == 0 && num2[i] == 1 && carry == 1)
{
c[j] = 0;
carry = 1;
sum[k] = 0;
result = sum[k];
}
else if (num[i] == 1 && num2[i] == 0 && carry == 1)
{
c[j] = 0;
carry = 1;
sum[k] = 0;
result = sum[k];
}
else if (num[i] == 1 && num2[i] == 0 && carry == 0)
{
c[j] = 1;
carry = 0;
sum[k] = 1;
result = sum[k];
}
else if (num[i] == 0 && num2[i] == 0 && carry == 1)
{
c[j] = 1;
carry = 0;
sum[k] = 1;
result = sum[k];
}
else if (num[i] == 0 && num2[i] == 0 && carry == 0)
{
c[j] = 0;
carry = 0;
sum[k] = 0;
result = sum[k];
}
k--;
}
c[0] = carry;
cout << "\n" "The sum of the two binary numbers is: " << endl;
for(k = 0; k <= 3; k++)
{
cout << result;
}
cout << endl;
}
/*void binNum::multiplication()
{
for(i = 3; i <= 0 ; i--)
{
if (num[i] == '1' && num2[i] == '1' && carry == '1')
{
c[j] = 1;
carry = 1;
sum[k]=1;
}
else if (num[i] == 1 && num2[i] == 1 && carry == 0)
{
c[j] = 0;
carry = 1;
sum[k] = 0;
}
else if (num[i] == 0 && num2[i] == 1 && carry == 0)
{
c[j] = 1;
carry = 0;
sum[k] = 1;
}
else if (num[i] == 0 && num2[i] == 1 && carry == 1)
{
c[j] = 0;
carry = 1;
sum[k] = 0;
}
else if (num[i] == 1 && num2[i] == 0 && carry == 1)
{
c[j] = 0;
carry = 1;
sum[k] = 0;
}
else if (num[i] == 1 && num2[i] == 0 && carry == 0)
{
c[j] = 1;
carry = 0;
sum[k] = 1;
}
else if (num[i] == 0 && num2[i] == 0 && carry == 1)
{
c[j] = 1;
carry = 0;
sum[k] = 1;
}
else if (num[i] == 0 && num2[i] == 0 && carry == 0)
{
c[j] = 0;
carry = 0;
sum[k] = 0;
}
k--;
}
c[0] = carry;
cout << "\n" "The sum of the two binary numbers is: " << endl;
for(k = 0; k <= 3; k++)
{
cout<< num[k];
}
cout << endl;
}*/
void binNum::convert (int binary)
{
int hold = 1;
int final = 0;
while (binary != 0)
{
if (binary % 2 == 1)
{
binary = (binary-1)/10;
final += hold;
}
if (binary % 2 == 0)
{
binary = binary/10;
}
hold *= 2;
}
cout << "Base 10 conversion: " << final << endl;;
}
binNum::~binNum() // Destructor function definition
{ cout << "press enter to exit";
}