I've been struggling with a problem for a while. As I'm reading numbers from a .txt file, the algorithm keeps ignoring the last number from the queue and I don't understand why. How can I rewrite it? I've considered using !f.eof() as well, but I still have doubts. The algorithm should count how many sequences are of even numbers.
input: 1 2 3 4 6 10 2 8 5 7 9 4 6 2 4 8 121 20 4 11 10 2 5 2 6 8 10 16
output: 2
expected output: 3
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("matrice.txt");
int main()
{
long a, b, s=0, smax=-1, p;
fin>>a;
while(fin>>b)
{
if(a%2==0 && b%2==0)
if(s==0) s+=2;
else s+=1;
else
{
if(s>smax)
{ p=1; smax=s;}
else if(s==smax) p++;
s=0;
}
a=b;
}
cout<<p;
return 0;
}