So I'm writing a file that collects a list of integers from a .dat file and counts how many odds and evens there are.
The code I wrote for this works for the most part. But there is one obscure scenario where it fails. If a line in the .dat file ends in an even number it will count 1 more even and 1 less odd then its supposed to.
Any ideas as to whats happening? what's I tried:
cout << "What is the name of the input file? ";
cin >> fileName;
infile.open(fileName);
if (infile.fail())
{
cout << "Error opening " << fileName << endl;
}
else
{
infile >> number;
while (!infile.eof())
{
count1++;
infile >> number;
if (number % 2 != 0)
{
odd++;
}
else
{
even++;
}
}
}
infile.close();
cout << "The number of integers is: " << count1 << endl;
cout << "There are " << odd << " odd integers and " << even << " even integers" << endl;
system("pause");
return 0;
}