I wrote a program for an assignment that should read data from a file on the computer then display the sum of both the positive integers and negative integers. However, when I run my program it goes into an infinite loop. I need to understand what I am doing wrong. Any help would be greatly appreciated.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
int even = 0;
int odd = 0;
int x;
inputFile.open("C:....\hw4dataf11.txt"); //I have attached the file to this post
if (!inputFile)
cout << "Error opening file.\n";
while(!inputFile.eof()){
if(x % 2 == 0){
even += x;
cout << "Number of even numbers is: " << even << endl;
even++;
}
else {
odd += x;
cout << "Number of odd numbers is: " << odd << endl;
odd++;
}
}
inputFile.close();
cout << "Program is finished. \n";hw4dataf11.txt
return 0;
}