class help
Dec 9, 2011 at 6:03am UTC
I am trying to print out the result of binary addition, but when I run the program nothing prints out. I have been stuck at this point for quite some time and would really appreciate any help.
main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#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(i);
//cout << i << endl;
//keeps the output window open
cout << "Press enter to continue \n" ;
cin.sync();
cin.ignore();
char ch = getchar();
return 0;
}
binNum.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef BINNUM_H
#define BINNUM_H
using namespace std;
class binNum
{
private :
char num [5], num2 [5], c [5];
int i, j, carry, result;
public :
binNum(); //Default constructor
void numbers();
void addition(int );
~binNum();
};
#endif
binnum.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include "binNum.h" //obtain prototype definitions
#include <iostream>
using namespace std;
binNum::binNum()
{
i = 0;
carry = 0;
result = 0;
}
void binNum::numbers()
{
cout << "Enter a 4 bit binary number entering only 0's and 1's: " ;
for ( i=0; i < 5; i++)
{
cin >> num[i];
break ;
}
cout << "Enter another 4 bit binary number entering only 0's and 1's: " ;
for (i = 0; i < 5; i++)
{
cin >> num2[i];
}
}
void binNum::addition(int i)
{
for (i = 3; i <= 0 ; i--)
{
if (num[i] == '1' && num2[i] == '1' && carry == '1' )
{
c[j] = '1' ;
carry = '1' ;
result = '1' ;
}
else if (num[i] == '1' && num2[i] == '1' && carry == '0' )
{
c[j] = '0' ;
carry = '1' ;
result = '0' ;
}
else if (num[i] == '0' && num2[i] == '1' && carry == '0' )
{
c[j] = '1' ;
carry = '0' ;
result = '1' ;
}
else if (num[i] == '0' && num2[i] == '1' && carry == '1' )
{
c[j] = '0' ;
carry = '1' ;
result = '0' ;
}
else if (num[i] == '1' && num2[i] == '0' && carry == '1' )
{
c[j] = '0' ;
carry = '1' ;
result = '0' ;
}
else if (num[i] == '1' && num2[i] == '0' && carry == '0' )
{
c[j] = '1' ;
carry = '0' ;
result = '1' ;
}
else if (num[i] == '0' && num2[i] == '0' && carry == '1' )
{
c[j] = '1' ;
carry = '0' ;
result = '1' ;
}
else if (num[i] == '0' && num2[i] == '0' && carry == '0' )
{
c[j] = '0' ;
carry = '0' ;
result = '0' ;
}
j--;
}
c[0] = carry;
cout << "\n" "The sum of the two binary numbers is: " << endl;
for (j = 0; i <= j; j++)
{
cout << c[j];
}
cout << endl;
}
binNum::~binNum() // Destructor function definition
{ cout << "press enter to exit" ;
}
Topic archived. No new replies allowed.