Sum of evens and odds using while

Mar 5, 2008 at 12:12am
For class I need to write three programs; one using 'do-while', one using 'for', and another using just 'while'. I figured out the 'for' part of the assignment thanks to the book, but I'm a little foggy on using just 'while'. Here's what I have so far, forgive the crude nature of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main ()
{
	int number, sumeven, sumodd;

	cout << "Enter data for processing" << endl;
	cin >> number;

	sumeven = 0;
	sumodd = 0;

	while (number % 0)
	{
		cin >> number;
		sumeven = sumeven + number;
	}

	cout << "The sum of the even numbers = " << sumeven << endl;

	if (number % 1)
		sumodd = sumodd + number;
		cout << "The sum of the odd numbers = " << sumodd << endl;

	return 0;
}


I need to enter a series of integers, so limiting the number of integers added is part of the code as well. That was easy enough using 'for', but its a bit cloudy using 'while'.
The code compiles just fine, but the program crashes when run. I have a feeling I'm missing something simple. Can someone give me a little push over the edge, so to speak?

Thanks in advance!
Last edited on Mar 5, 2008 at 12:16am
Mar 5, 2008 at 12:27am
Ok, I modified the code a bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

const int N = 10;

int main ()
{
	int number, sumeven, sumodd, counter;

	cout << "Enter ten integers for processing:" << endl;
	cin >> number;

	sumeven = 0;
	sumodd = 0;
	counter = 0;

	while (N <= counter)
	{
		{
			if (number % 0)
		sumeven = sumeven + number;
		counter++;
		}
	
		{
			if (number % 1)
		sumodd = sumodd + number;
		counter++;
		}
	}
		cout << "The sum of the even numbers = " << sumeven << endl;

		cout << "The sum of the odd numbers = " << sumodd << endl;

	return 0;
}


I'm getting the build warning 'potential mod by 0', and no matter what I put in for the integers, the output gives me zeros.
Mar 5, 2008 at 12:54am
This time, I am truly baffled. Check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

const int N = 10;

int main ()
{
	int number, sumeven, sumodd, counter;

	cout << "Enter ten integers for processing:" << endl;
	cin >> number;

	sumeven = 0;
	sumodd = 0;
	counter = 0;

	while (counter <= N)
	{
		cin >> number;

		switch (number % 2)
		{
		case 0:
		sumeven = sumeven + number;
		counter++;
		break;
		case 1:
		sumodd = sumodd + number;
		counter++;
		break;
		}
	}

		cout << "The sum of the even numbers = " << sumeven << endl;

		cout << "The sum of the odd numbers = " << sumodd << endl;

	return 0;
}


It is reading the first 12 integers, and it is subtracting 1 off the value of 'sumodd'. Any ideas?
Mar 5, 2008 at 1:42pm
The first cin you have on line 12 before entering the loop is completely ignored. It's not added to sumodd or sumeven, and it doesn't count toward the total quantity of numbers entered. Simply remove that line.

That accounts for one extra number- the other is because you're starting the counter at 0 and looping as long as it's less than or equal to ten- in other words, it's looping 11 times. You have two choices.

change "counter = 0;" to "counter = 1;"
or
change "while (counter <= N)" to "while (counter < N)"
I prefer the latter, although it's not at all a big deal.

Mar 5, 2008 at 6:57pm
thats weird, i remember doing that same exact program for my 120 course
Mar 5, 2008 at 11:35pm
You probably have the same book that I do! Though I hear its a popular program for a beginner's course.
I took out the first cin and changed the <= to < last night shortly after my last post, and everything seems to work fine. Space knows I'll be back when I'm writing the 'do-while' code.
Thanks for the help!
Topic archived. No new replies allowed.