Odd Number Program with File Output?

Okay so what I need to do is write a program that would create a file called "odd.txt" that contains odd numbers 1-99, one number per line.

So I wrote this code that would take an input file containing the numbers 1-99, one per line like so:
1
2
3
4
5
1
2
3
4
etc.


and output the odd ones by finding the remainder of the number divided by two and then sorting those that had 1 for a remainder into the output file.

My problem is that my program is outputting infinite ones into the output file and I have no idea how to get it to check every number instead of just repeatedly checking the first number. Using the while loop I have in there is what got it to start outputting only ones. Previously it wasn't outputting at all.

Here is my full 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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin("numbers.txt");
    ofstream fout("odd.txt");

        //check for possible errors

    if (fin. fail())
    {
        cerr << "could not open input file numbers.txt\n";

    }
    if (fout. fail())
    {
        cerr << "could not open output file odd.txt\n";

     }

    int  x;
    fin >> x;
while (!fin.eof())
{
         if (x % 2 ==1)
         {
             fout << x << "  " << endl;
         }

        else
        {
            cerr << "Even number"
            << x << " " << endl;
        }
}

    fin.close();
    fout.close();

    return 0;
}


Any help at all would be greatly appreciated.
Last edited on
1
2
3
4
5
fin >> x;
while (!fin.eof())
{
/*Here you don't touch fin*/
}
Infinite loop
Topic archived. No new replies allowed.