Hello everybody, really need some help getting this code to add up even and odd numbers from a set of integers. But it also needs to default or continue through if input is incorrect (i.e.- $,@,#,3.4) and so on. Here is what I have so far. Thank you.

#include <iostream>
#include <iomanip>

using namespace std;

const int N = 20;

int main()
{
		//Declare Variables
	int counter;
	int number;
	int zeros;
	int odds = 0;
	int sumOdds = 0;
	int evens = 0;
	int sumEvens = 0;

	cout << "Please enter " << N << " integers, Positive or Negative " << endl;

	cout << "The numbers you entered are:" << endl;

	for (counter = 1; counter <= N; counter++) 
	{
		cin >> number;
		cout << number << " ";

		switch (number % 2)
		{
		case 0:
			evens++;
			if (number = evens)
				sumEvens = number + number;
			if (number == 0)
				zeros++;
			break;
		case 1:
		case -1:
			odds++;
			if (number = odds)
				sumOdds = number + number;
		}
	}

	cout << endl;

	cout << "There are " << evens << "Evens" << endl;
	cout << "Sum of Evens = " << sumEvens << endl;
	cout << endl;
	cout << " There are " << odds << " Odds" << endl;
	cout << "Sum of Odds = " << sumOdds << endl;

	return 0;
}

line 23, set counter to 0.
line 32, your if statement is syntatically incorrect. you need ==. Also, you don't need that if statement.
Line 33, sumEvens = number + number? Think about that for a sec. If the number happens to be 2, sumEvens will be 4. The next iteration, number is 16. sumEvens will be 32. 16+2 != 32. You need to include the variable sumEvens in the addition.
line 38, you will never have a case of -1, you can erase that line.
line 37 once you get rid of 38, it will flow correctly.
line 40 your if statement is again wrong and why even have it?
line 41 same problem as line 33.

Edited 6 Years Ago by kes166: n/a

Comments
very clear and understandable instructions

line 23, set counter to 0.
line 32, your if statement is syntatically incorrect. you need ==. Also, you don't need that if statement.
Line 33, sumEvens = number + number? Think about that for a sec. If the number happens to be 2, sumEvens will be 4. The next iteration, number is 16. sumEvens will be 32. 16+2 != 32. You need to include the variable sumEvens in the addition.
line 38, you will never have a case of -1, you can erase that line.
line 37 once you get rid of 38, it will flow correctly.
line 40 your if statement is again wrong and why even have it?
line 41 same problem as line 33.

Hey thanks for the input, just found out that I need to create a EOF loop. But I probably will need help in the very near future lol. Thanks

int main()
{
   int n, num, sumo=0, sume=0, numo=0, nume=0;
   cout<<"Enter n integers: ";
   cin>>n;
   for(int i=0; i<n; ++i)
   {
     cin>>num;
     if(num%2==0)
     { nume++; sume = sume + num; }
     else
     { numo++; sumo = sumo + num; }
   }
   cout<<"Out of "<<n<<" integers "<<numo<<" are odd & "<<nume<<" are even"<<endl;
   cout<<"sum of odd integers is: "<<sumo<<" & sum of even integers is: "<<sume;  
}
This question has already been answered. Start a new discussion instead.