how do create an outfile that will contain only the even numbers from the infile txt
heres the infile numbers
40
13
25
58
55
12
20
9
3
6
8
12
the outfile can only contain the even numbers
how do create an outfile that will contain only the even numbers from the infile txt
heres the infile numbers
40
13
25
58
55
12
20
9
3
6
8
12
the outfile can only contain the even numbers
1. Read the infile
2. Store the even numbers
3. Create the outfile
4. Write the even numbers
Please show the code you have attempted so far.
Woop?
anyone???
O_o
I understand your confusion. I'll explain. With "show the code you have attempted" prog-bman is asking you to show the code you have attempted.
You'll be surprised by how readily the regulars here will help you if you show a willingness to help yourself.
Soma
Code:while (infile.eof()== false) { infile >> number; infile >> evenNumber; if (num%2 == 0) outfile << evenNum << endl; }
o_O
A complete example if you please.
Why are you reading into two variables?
Soma
i have to get this number list
which is located in the infileCode:40 13 25 58 55 12 20 9 3 6 8 12
and i have to write only the even numbers to the outfile
my questio is how do i figure out how to only get the even ones...i keep getting the wrong numbers in the outfile txt
If that example is truly representative of your code, it is no wonder that you get the wrong values.
I'll ask again. What code are you using?
You need to inspect every value to determine if the value is even.
Soma
Couple of bugs in that posted code.
Should be replace withCode:while (infile.eof()== false)
Reason is answered in the FAQ, but basically using eof with cause you to read in more characters than you want.Code:while(infile >> number)
Also this line
Does not need to be there. You just need to check if number is even and then output it.Code:infile >> evenNumber;
Woop?
ok and how do i check for even numbers using what code and how do i code to put it into the outfile...
Using the code you already posted.
Code:if(number % 2 == 0){ outfile << number<< endl; }//if
Woop?
this is what i get in the outfile txt
13
9
12
clearly not the even numbers
Post the code as it stands now, so I can check it out.
Woop?
here the numberList.txtCode:#include<iostream> #include<fstream> using namespace std; int main() { short number = 0; short evenNumber = 0; ofstream outfile; ifstream infile; outfile.open ("evennumList.txt",ios.app); infile.open ("numberList.txt",ios.in); if (infile.is_open()== false) cout << "Error"<<endl; else while (infile.eof()== false) { infile >> num; infile >> evenNum; if (num%2 == 0) { outfile << evenNum << endl; } } infile.close(); outfile.close(); return 0; }
Code:40 13 25 58 55 12 20 9 3 6 8 12
I am going to re-quote myself.
Couple of bugs in that posted code.
Code:
while (infile.eof()== false)
Should be replace with
Code:
while(infile >> number)
Reason is answered in the FAQ, but basically using eof with cause you to read in more characters than you want.
Also this line
Code:
infile >> evenNumber;
Does not need to be there. You just need to check if number is even and then output it.
Woop?