Fstream: Returning even integers to txt?

I'm writing a program that returns the even integers from a txt file containing even and odd intgers to a new txt file. But i'm have trouble returning the even integers to the new txt file.

Here the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void FindFile( ofstream &outF)
  {
  ifstream fout;
  ofstream fouts;
  string filename1, filename2;
  int x;


  cout << "Enter input file name: ";
  cin >> filename1;
  cout << endl;

  cout << "Enter output file name: ";
  cin >> filename2;
  cout << endl << endl << endl << endl;




fout.open(filename1.c_str());
if (!fout)
{
cout << "ERROR: File " << QUOTES << filename1 << QUOTES << " Program Aborted" << endl;
cout << endl << endl << endl;

}

fouts.open(filename2.c_str());
  if(!fouts)
     {
      cout << "Can't open output file" << filename2 << "." << endl;
   
     }
 if(fout.good() )
   {
    cout << "Proccessing Data . . ." << endl;
    cout << ". . . Data processing completed." << endl; //
    }

while( fout >> x)
    {
   fouts << x << endl;     // here's the problem part. with the loop.
   EvenTest(x);             
    }

}
void EvenTest( int number)
{
 
  if( number % 2 == 0 )
    {
    cout << number << " is even." << endl;
    
    }
 else 
    {
    cout << number << " is odd." << endl;
  
    }
Hi there! you seem to be lacking an "if" before line 42, that asks whether or not the number is an even integer, and procede to save it in the new file, or continue if the answer is a negative.

You can solve that by making EvenTest() to return a bool, and call it before line 42 like the argument for the if.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
while( fout >> x)
    {
   if ( EvenTest(x) )
        {
      fouts << x << endl;     // here's the problem (now solved)part. with the loop.
        }             
    }

}

bool  EvenTest( int number)
{
 
  if( number % 2 == 0 )
    {
    cout << number << " is even." << endl;
    
    }
 else 
    {
    cout << number << " is odd." << endl;
  
    }
   return number % 2 == 0;
}
Last edited on
Thank you! I figured it out later but thank you for the reply.
Topic archived. No new replies allowed.