Hello Everybody
I cannot get my file to open in my eof program. Outlined below is what the program needs to do:
1) Program needs to read from a file (ex. "Data.txt")
2) Program needs to read even & odd numbers down file. (ex. 2
3
12
5)
3) Program needs to add even numbers and display sum
4) Program needs to ass odd numbers and display sum also
5) Program needs to close properly
Here is what I have so far:
// Author:
// Ch.5 Exercise 6
// Program: EOF-even/odd
// Oct. 4, 2010
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Delcare Variables
int i;
int evenSum = 0;
int oddSum = 0;
//Comment: Declare stream variables
ifstream inFile;
//Comment: Open Input File
inFile.open("Data.txt");
//Comment: Test Opening File
if (!inFile.is_open())
{
cout << "There was a problem opening file "
<< endl;
return 1;
}
//Comment: File Read Operations
while (!inFile.eof())
{
//Comment: Adding Even & Odds From
// File.
if (i % 2 == 0) evenSum += i;
if (i % 2 > 0) oddSum += i;
}
// Comment: Display Even Sum & Odd Sum
cout << "Evens: " << evenSum << endl;
cout << "Odds: " << oddSum << endl;
// Comment: Close of File
inFile.close();
return 0;
}