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;
}
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???
#include <iostream>
usingnamespace std;
constint 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.