coding using while and sentinel

hi,

I wrote the code and it runs correctly. The very end result is the one that I need. I dont want it to print the stuff in between....just the final sum of even and final sum of odd. I'm not sure what I am doing wrong. Is it the format?

the program that i am trying to write is:

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

• Read the integers in from user input
• Use a sentinel-controlled while loop to control input
• Do not attempt to include input validation. For this assignment input validation would be pretty tricky and is likely to cause problems. You may assume for this assignment that the user will enter only valid integer values
• Users may enter negative numbers as well as positive numbers and/or zero. The only integer that should be “off limits” is the sentinel value used to control the while loop.
• Write your output to the screen

here is my code:

#include <iostream>

using namespace std;

const int SENTINEL = -999;

int main()
{
int integers = 0;
int sum = 0;
int even = 0;
int odd = 0;


while (integers != SENTINEL)
{

cout << "Enter an integer ending with -999: " << endl;
cin >> integers;


if (integers % 2 == 0)

{
even = even + integers;
}

else if (integers % 2 != 0)

{
odd = odd + integers;
}



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

system("pause");
return 0;
}

If you do not want to print
1
2
cout << "The sum of your even integers is: " << even << endl;
cout << "The sum of your odd integers is: " << odd << endl;

each iteration you should get them off while loop :D
not to sound ignorant, but to remove it from the while loop would be to place it under the bracket directly underneath it correct? but if I do that the math doesn't compute properly???
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;

const int SENTINEL = -999;

int main() {
	int integers = 0;
	int even = 0;
	int odd = 0;

	cout << "Enter an integer ending with -999: " << endl;

	while (integers != SENTINEL) {
		cin >> integers;
		if (integers % 2 == 0) {
			even = even + integers;
		}
		else {
			odd = odd + integers;
		}
	}

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

	return 0;
}


But it will add -999 number. You need to change code so it would not add it.
Thank you ! You are Awesome :)
Topic archived. No new replies allowed.