Dear friends,
I want to create a program that reads 10 numbers from the user and store it in a file namely Numbers.txt.
After that I have to read each numbers and if it is a Even number then it should be stored in a file "Even.txt" otherwise it should be stored in a file "Odd.txt".
I craeted the program and while executing the files are created correctly but when it is reading and displaying the odd and even numbers from their file the last number is printed twice. I used eof() and good() function but there is no change in the output.
so please tell me why it is printed twice and also how to rectify it ?

The programs is shown below

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;

int main()
{

     int x,i ,n;
     fstream fout("Numbers.txt",ios::out);
     cout<<"Enter the Numbers: \n";
     for(i=0;i<10;i++)
     {
          cin>>x;
          fout<<x<<"\n";
     }
     fout.close();
     fstream fin("Numbers.txt",ios::in);
     fstream outfile2("Even.txt",ios::out);
     fstream outfile3("Odd.txt",ios::out);
     for(i=0;i<10;i++)
     {
          fin>>n;
          if(n%2==0)
             outfile2<<n<<"\n";
          else
            outfile3<<n<<"\n";
     }
     fin.close();
     outfile2.close();
     outfile3.close();
     fstream infile2("Even.txt",ios::in);
     cout<<"\nEven File Contains:";
     while(infile2.good())
     {
      infile2>>n;
      cout<<"\n"<<n;
     }
     fstream infile3("Odd.txt",ios::in);
     cout<<"\nOdd File Contains:";
     while(infile3.good())
     {
       infile3>>n;
       cout<<"\n"<<n;
     }
     return 0;
}

I used eof() and good() function but there is no change in the output.

They're both wrong, so I'm not surprised. The problem is that both eof() and good() will only switch state after you've tried to read from the file and that attempt fails. So you need some way to check the state after reading but before processing what was read, otherwise you'll accidentally process the previously read item again.

Convention is this:

while (infile2 >> n)
    cout << '\n' << n;

The extraction operator will return a reference to the stream object, which when used in boolean context will give you the current state. Alternatively you can still use the eof() or good() option, but a conditional inside the loop is needed:

while (infile2.good()) {
    if (!infile2.good())
        continue;

    cout << '\n' << n;
}

That's redundant, verbose, and logically confusing, so I don't recommend it.

Thank you deceptikon Amazing, it works correctly,but the second method is not working it is printing the last entered number infinitely..

but the second method is not working it is printing the last entered number infinitely..

My bad, I forgot to include the actual input request:

while (infile2.good()) {
    infile2 >> n;

    if (!infile2.good())
        continue;

    cout << '\n' << n;
}

My bad, I forgot to include the actual input request:

Thanks again, it works........I think In if statement we are checking the next position is good or not?
Am I Right?....

Thanks again, it works........

Please don't use that longer loop for anything but your own instruction. The conventional method is strongly recommended.

I think In if statement we are checking the next position is good or not?

No, we're checking if the previous input request tried to read of the end of the stream or encountered an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.