Sum of unknown amound of even and odd variables

This is my assignment
Write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. Assume that the input comes from the keyboard (interactive) and the output goes to the monitor.

And this is what I currently have done:

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

bool isEven(int integer);

int main ()
{
int integer;
int evensum;
int oddsum;

while (!cin.eof()){

cout << "Please enter an integer (Ctrl+Z to quit)";
cin >> integer;

if(integer % 2 == 0){
evensum = evensum + integer;
cout << "\n" << evensum;
}
else{
oddsum = oddsum + integer;
}

}

cout << "Sum of even integers = " << evensum - 1990030162;
cout << "Sum of odd integers = " << oddsum + 2;

return 0;
}

I don't know why but I'm getting results that are off. I think I fixed it with odd numbers by adding 2 to the answer but with even numbers a 0 alone gets -8, a 2 alone gets -4, a 4 alone gets 0, and an 8 alone gets 8. I don't understand what causes the wide variety of results.
Line 12-13: What do you think the value of evensum and oddsum are? Hint: Garbage.

Line 21,25: What do you thing the running sums are when the starting value of each is garbage?

Line 15,18: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (cin >> var) 
  {  //  Good cin operation
  }


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.




Thanks that helped a ton. And I will remember to use the code tags next time.
Topic archived. No new replies allowed.