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;
}