As a homework assignment, I was asked to write a program in C++ that calculates the sum of the even and odd integers in a list of integers given by the user. I think I'm on the right track, but I can't seem to get my loop right. Here's what I have so far:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int num=0, even=0, odd=0, i = 0;
	char stop = 'a';

    cout << "Enter a list of integers followed by the '$' sign: " << endl;                 
    cin >> num;
	stop = num;

		while(stop != '$')
		{
			if(num % 2 == 0) //If the number is even
				even = even + num; //Add it to the current sum of even integers
			else if(num % 2 != 0) //if the number is odd
				odd = odd + num; //Add it to the current sum of odd integers
			else
			{
				i = 1; //Otherwise, make i equal to 1
			}

			cin >> num;
			stop = num;
		}

	if(i == 1)
		cout << "Please enter only integers" << endl;
	else if(i == 0)
	{
		cout << "The sum of the even integers is: " << even << endl;
		cout << "The sum of the odd integers is: " << odd << endl;
	}

	return 0;
}

Thanks for the help :)

Look at line 13

stop = num;

Now ask yourself, "self, whats stop?"
"a character."
"self, whats num?"
"an integer."

Whats probably going down is that the the char (stop) is being assigned an integer, which in effect is tossing your loop off.

Are you only interested in positive integers? Because then you could ask for a negative value to terminate that list of integers.

while(num != -1)

The question states: Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers.

I'm assuming it means positive and negative integers. Any ideas?

Nvm, I was going about it wrong. I figured it out. Here's what I have:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int even = 0, odd = 0, dig, num;

	cout << "Please enter a list of numbers (ex. - 12345)" << endl;
	cin >> num;

	while (num > 0)
	{
		dig = num % 10;
		num = num / 10;
		
		if(dig % 2 == 0)
			even = even + dig;
		else
			odd = odd + dig;
	}

	cout << "The sum of the even integers is: " << even << endl;
	cout << "The sum of the odd integers is: " << odd << endl;

	return 0;
}

Thanks for the help :)

Nvm, I was going about it wrong. I figured it out. Here's what I have:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int even = 0, odd = 0, dig, num;

	cout << "Please enter a list of numbers (ex. - 12345)" << endl;
	cin >> num;

	while (num > 0)
	{
		dig = num % 10;
		num = num / 10;
		
		if(dig % 2 == 0)
			even = even + dig;
		else
			odd = odd + dig;
	}

	cout << "The sum of the even integers is: " << even << endl;
	cout << "The sum of the odd integers is: " << odd << endl;

	return 0;
}

Thanks for the help :)

what is quite confusing is the 'num' integer.
You said that you are taking input of a list of integers while you have just asked for a single integer.
I assume that you have taken the input of "list of integers" in a single integer 'num' itself, i.e., you have considered the value of 'num' as a list of integers (digit by digit) :|
if this is what you have assumed, i think this is a wrong approach. You would have rather used an array. :)
Think upon it.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int even = 0, odd = 0, dig, num;

	cout << "Please enter a list of numbers (-7777 to terminate)" << endl;
	cin >> num;

	while (num != -7777)  // off the wall check value
	{
		
		// dig = num % 10;
		// num = num / 10;           <--- what are these for?


		
		if(dig % 2 == 0)       // if the number is even
			even += num; // add it to the previous even sum						
		else
			odd += num;   // otherwise add it to the odd sum

		cout << "Enter next number: ";
		cin >> num;
	}

	cout << "The sum of the even integers is: " << even << endl;
	cout << "The sum of the odd integers is: " << odd << endl;

	return 0;
}

I haven't learned arrays yet :\

And what if the user wants to input -7777 as part of the list?

I dunno what I should do with this program :\

K, class has arrived. Thanks for the help =)

I decided to use the example with the -7777 terminating value =)

This article has been dead for over six months. Start a new discussion instead.