Adding odd and even numbers

Hi guys. Ive tried several ways of doing this program, taking 4 numbers, then adding the even together , then the odd. I keep getting weird answers. Also, I'm sure you can convert the value of one variable to another, but was wondering on the rule, which goes on the left of the = sign, and which goes on the right.

#include <iostream>
#include<cmath>

using namespace std;

int main()
{
int number1,number2,number3,number4;
int even1,even2,even3,even4;
int odd1,odd2,odd3,odd4;
int totalEven;
int totalOdd;



cout<<"Please enter 4 numbers, and we will add the even's together, and the odd's together "<<endl;
cout<<"Enter your first number "<<endl;
cin>>number1;
cout<<"Enter your second number "<<endl;
cin>>number2;
cout<<"Enter your third number "<<endl;
cin>>number3;
cout<<"Enter your fourth number "<<endl;
cin>>number4;

if(number1 % 2 == 1) //modulus gives remainder;
//if you divide first number 2 and get a remainder of 0, its even. If remainder of 1, its odd
{
odd1= number1;
}
if(number2 % 2 == 0)
{
even1=number1;
}
if(number2 % 2 == 1)
{
odd2=number2;
}
if(number2 % 2 == 0)
{
even2=number2;
}

if(number3 % 2 == 1)
{
odd3=number3;
}
if(number3 % 2 == 0)
{
even3=number3;
}

if(number4 % 2 ==1)
{
odd4= number4;
}
if(number4 % 2 ==0)
{
even4= number4;
}

totalEven = even1+ even2+even3+even4;
totalOdd = odd1+odd2+odd3+odd4;
cout<< " The sum of the even numbers is "<<totalEven<<endl;

cout<<" The sum of the odd numbers is "<<totalOdd<<endl;




return 0;
}
The output is
The sum of the even numbers is 4310147
The sum of the odd numbers is 1973340282
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    std::cout << "Please enter 4 numbers, and we will add the even's together, "
              << "and the odd's together\n" ;
    int n1, n2, n3, n4 ;
    std::cin >> n1 >> n2 >> n3 >> n4 ;

    const int sum_all_numbers = n1+n2+n3+n4 ;
    int sum_even_numbers = 0 ; // note: initialise to zero

    if( n1%2 == 0 ) sum_even_numbers += n1 ; // note: +=
    if( n2%2 == 0 ) sum_even_numbers += n2 ;
    if( n3%2 == 0 ) sum_even_numbers += n3 ;
    if( n4%2 == 0 ) sum_even_numbers += n4 ;

    const int sum_odd_numbers = sum_all_numbers - sum_even_numbers ;

    std::cout << "the sum of the even numbers is " << sum_even_numbers << '\n'
              << "and the sum of the odd numbers is " << sum_odd_numbers << '\n' ;
}
Thanks, JLBorges, now I need to go through your code line by line to understand it. The std::cout isnt necessary if I put the "using namespace std;" at the top, right? I've seen this form a lot, still don't quite understand why it's not all done the same.
> The std::cout isnt necessary if I put the "using namespace std;" at the top, right?

Right.


> I've seen this form a lot, still don't quite understand why it's not all done the same.

Re. what namespaces are and the meaning of using namespace std,
see: http://www.cplusplus.com/forum/beginner/142171/#msg750694

Re. why it's not all done the same
see: http://www.cplusplus.com/forum/beginner/216084/#msg1002642
Thanks a lot, I'm starting to have my normal day thinking being preceded by cout<<, so I guess I need a break, but this gives me a direction to understanding this program (I'm only 4 weeks in the class).
Registered users can post here. Sign in or register to post.