1. ## c++ : binary addition

hi i am biggnner to programming
so i don't know exactly how to add binary numbers (5bit)
i tried of course

Code:
```#include <iostream>
using namespace std;

int main ()
{

int arr[5],arr1[5];
int carry=0,sum,sum[6],i;
cout<<"number one : ";
for ( i=0;i<5;i++)
{
cin>>arr[i];
}
cout<<"\n number two: ";
for ( i=0;i<5;i++)
{
cin>>arr1[i];
}
for ( i=0;i<5;i++)
{
if (arr[i]==0 && arr1[i]== 0 && carry== 0)
sum=0;
else if (arr[i] == 0 && arr1[i]==1 && carry== 0)
sum=1;
else if (arr[i]== 1 && arr1[i] == 0 && carry == 0)
sum=1;
else if (arr[i] == 1 && arr1[i] == 1 && carry ==0)
{
sum=0;
carry=1;
}
else if (arr[i] == 0 && arr1[i] == 0 && carry == 1)
{
sum=0;
carry=0;
}
else if (arr[i] ==0 && arr1[i] == 1 && carry ==1 )
sum=0;
else if (arr[i] == 1 && arr1[i] == 0 && carry == 1)
sum=0;
else if (arr[i] == 1 && arr1[i] == 1 && carry ==1)
sum=1;
else
cout<<" WRONG!";
}
sum[i]=sum;
}

for(i=0;i<6;i++)
{
cout<<" sum = "<<sum[i]<<endl;
}
return 0;
}```
what's wrong with it ? can you explain it
and if there is another way to add binary numbers light me plzzz

2. this algorithm should work
Code:
```0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 10
1 + 1 + 1 = 11```

3. ok thanks i will try it

but how can make sure that the input in binary ?
what should i write

4. The main 8 part of the "if/else if" have to set the correct value for "carry".

I suggest working on adding without using the input code till you get adding to partly work.

Code:
```    int arr[5] = {1,0,0,0,0}; // Binary 00001
int arr1[5] = {1,1,0,0,0}; // Binary 00011```
Code:
```    cout<<" arr = "<<arr[4]<<arr[3]<<arr[2]<<arr[1]<<arr[0]<<endl;
cout<<" arr1 = "<<arr1[4]<<arr1[3]<<arr1[2]<<arr1[1]<<arr1[0]<<endl;```
Code:
`    cout<<" sum = "<<sum[5]<<sum[4]<<sum[3]<<sum[2]<<sum[1]<<sum[0]<<endl;`
FYI: You last "for" loop is NOT inside the main function; I suugest trying to get code to compile without errors before posting it.
Or, post the errors and warnings if it has them.

5. i have exam soon & i need to know how to do it with input

6. plzz help me ((

7. ok i changed my code to this
Code:
```#include <iostream>
using namespace std;
int main ()
{
int arr[5],arr1[5],carry=0,sum[6],i;
cout<<"number 1. ";
{
cin>>arr[i];
}
cout<<"\n number 2. ";
for (i=0;i<5;i++)
{
cin>>arr1[i];
}
for (i=0;i<5;i++)
{
if (arr[i] + arr1[i] + carry == 0)
sum[i]=0;
else if (arr[i] + arr1[i] + carry == 1)
sum[i]=1;
else if ( arr[i] + arr1[i] + carry == 2)
sum[i]=0;
carry=1;
else if (arr[i] + arr1[i] + carry == 3)
sum[i] =1;
carry=1;
else
cout<<"wrong!";
}
for (i=0;i<6;i++)
cout<<"sum= "<<sum[i];
return 0;
}```
errors
Code:
```*******************\nn.cpp(24) : error C2181: illegal else without matching if
*******************\nn.cpp(27) : error C2181: illegal else without matching if```

8. So there's one little issue and two big issues here. Little issue: in case 5 I don't think you want to claim that 0+1=0. Big issue #1: you do know you have to add from right to left? Big issue #2: If you don't have your user type spaces between the digits you'll get the whole number in arr[0] rather than one digit in each. If you don't want the user to type spaces, then you're going to have to read character by character.

9. in case 5 I don't think you want to claim that 0+1=0.
ok how can i correct it ?

Big issue #1
yes i know

Big issue #2
i know but i wasn't sure how to seperate them and adding them
by using % / ?

like i said i am biggnner to the programming
c++ is my first language

10. Originally Posted by Soul
ok how can i correct it ?
I would suspect claiming that 0+1=1 instead would be a good start.

Originally Posted by Soul
i know but i wasn't sure how to seperate them and adding them
by using % / ?
Just as reasonable as anything else, I suppose.

11. Until you learn to indent your code, you SHOULD always use "{" and "}" in your if statements!!

And, to repeat my prior comment; YOU MUST set carry to a valid value in each part of the if statement; not just the part where it is 1!!!!!!!!!!!!!!!!!!!!

Edit: Maybe you will understand this; you only set carry to 1 inside the loop; how will it every change back to 0 inside the loop if you do not change it.

Tim S.

Code:
```int main()
{
int arr[5],arr1[5],sum[5],carry[6]={0};
int i ,c;

cout<<"number 1.:"<<endl;
for(i=0;i<5;i++)
{
cin>>arr[i];
}
cout<<"number 2.:"<<endl;
for(i=0;i<5;i++)
{
cin>>arr1[i];
}

for(i=0;i<5;i++)
{
c=arr[i]+arr1[i]+carry[i];
if(c==0)
{
sum[i]=0;
carry[i+1]=0;
}
else if(c==1)
{
sum[i]=1;
carry[i+1]=0;
}
else if(c==2)
{
sum[i]=0;
carry[i+1]=1;
}
else if(c==3)
{
sum[i]=1;
carry[i+1]=1;
}
}
cout<<"sum"<<""<<"carry"<<endl;
for(i=0;i=4;i++)
{
cout<<sum[i]<<""<<carry[i+1]<<endl;
}
return 0;
}```
no errors but the loop won't end :/

13. No errors, but a warning:
Code:
`warning: suggest parentheses around assignment used as truth value`
On this line (with an error in it):
Code:
`for(i=0;i=4;i++)`

14. Code:
`for(int i=0;i=4;i++)`

15. Originally Posted by xentaka
Code:
`for(int i=0;i=4;i++)`
Eh, i was already declared at the very top for all the for loops. So that's okay.