I'm supposed to write a program that reads positive integers from a file and finds the sum of the even numbers as well as the sum of the odd numbers and outputs it.
I can't get the input file to be read correctly, right now it just outputs a very large negative number. I've looked at different forums for help but I still don't understand.
This is the code I have so far:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
int evenSum = 0, oddSum = 0;
int num;
infile.open("integers.txt");
//infile >> num;
//cout << "The numbers entered are: " << endl;
//cout << num;
while (infile >> num)
{
//num++;
if (num % 2 == 0)
evenSum = evenSum + num;
else
oddSum = oddSum + num;
//infile >> num;
}
cout << "The numbers entered are: " << endl;
cout << num;
cout << "\n\n\nEven sum: " << evenSum << endl;
cout << "Odd sum: " << oddSum << endl;
infile.close();
return 0;
}
I have some lines comment-blocked out because I was testing things and I'm not sure if I need it or where to put those things.
For reference here are the integers from the file:
30 20 15 17 22 26 29 33 35 86 40 41 49 23 11 9 6 55 16 77
Here is also a sample output of what it should look like:
any help or feedback is highly appreciated