954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with character array and binary addition

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"; 
}
emitremmit
Newbie Poster
6 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

On line 50 you are using j without initializing it. Same thing goes for every other time.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 
On line 50 you are using j without initializing it. Same thing goes for every other time.

I initialized j = 0 and now my output is just outputting a blank line.

emitremmit
Newbie Poster
6 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Look at your loop definition. Look at the code. Compare variables.

Also, if you add num[i], num2[i], and carry, you get a value from 0 to 3. Look at your IFs to see if you do different things when you get 2. Or 1. etc. If you don't do different things, you probably don't need so many comparisons.

I'll bet you can compress that whole section (48-109) into less than 10 lines with a little thought.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

thanks for the help I figured it out

emitremmit
Newbie Poster
6 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: