I have written the following code. What I did is this
- opened a file "Numbers.dat" and asked input from console
- then separated the numbers in separate even and odd files
#include <fstream>
#include <iostream>
int main() {
std::ofstream numWrite("Numbers.dat");
int temp;
while(std::cin>>temp){
numWrite<<temp<<std::endl;
}
numWrite.close();
std::ofstream even("Even.dat"), odd("Odd.dat");
std::ifstream num("Numbers.dat");
while(num){
num>>temp;
if(temp%2==0)
even<<temp<<std::endl;
else
odd<<temp<<std::endl;
}
num.close();
even.close();
odd.close();
return 0;
}
I used newlines after every input, therefore I am having the extra newline in Numbers.dat file and when my program is reading that, it is giving an extra output to the even/odd files.
I can eliminate them by either removing newline from the numbers.dat file, or some check on the other code. But I am unable to do that, please help! Or if there is a better way, tell me that too!
Inputs:
1 2 3 4 5 6 7 8 9 10 a
OUTPUTS:
even.dat
2
4
8
10
10
odd.dat
1
3
5
7
9