[try Beta version]
Not logged in

 
Please!! need help in outputing the even numbers of myvector into a file

Dec 16, 2016 at 12:37am
Hi guys,
I am new c++ learner, I have stuck in copying the even values of myvector into a outfile stream, the program prints the required result on screen but when you see the created file it doesn't pop up the even values of the vector.
I would appreciated if anyone could help me out in this regard. Below is my coding.

#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>

using namespace std;


void even_outputfile(vector <int> &numb,int);


int main()
{

int i, size=20;
vector <int> myvector;
for (i= 0; i<size; i++)
{
myvector.push_back(i);
cout<<"[ "<<myvector[i]<<" ]";
}
std:cout<<endl<<"\n";

even_outputfile(myvector,size);

return 0;
}


void even_outputfile(vector <int> &numb, int size)
{
int i;
for(i=0; i<size; i++)//loop to read even elements of the vector
{
if(numb[i]%2!=1)
{
std::cout<<"[ "<<numb[i]<<" ]";// prints the copied vector to the screen
}
}
// date type for output file
std::ofstream output_file;

//create an outfile name sorted
output_file.open("even.txt");

//copy data from vector to a file
ostream_iterator<int> output_iterator(output_file, " ");

std::copy(numb.begin(), numb.end(), output_iterator);
}
Last edited on Dec 16, 2016 at 1:08am
Dec 16, 2016 at 1:49am
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/algorithm/copy_if/
Dec 16, 2016 at 3:12am
int i, size = 20;
Move i within the for loop as it is only used there. Like this: for (int i = 0; i < size; i++)
Also, make size const, to prevent any accidental modifications.

std:cout << endl << "\n";
Missing a colon.
You don't need to prefix cout with std, when you're using namespace std
What's your purpose of mixing endl and '\n' ? If in doubt, prefer to use '\n', to avoid the unnecessary call to std::flush().

void even_outputfile(vector<int>& numb, int size)
You don't need to have the parameter size. vectors keep track of this by themselves.
http://www.cplusplus.com/reference/vector/vector/size/

1
2
    int i;
    for (i = 0; i < size; i++) //loop to read even elements of the vector 

Again, prefer to keep i within the loop.

1
2
3
4
    //copy data from vector to a file
    ostream_iterator<int> output_iterator(output_file, " ");

    std::copy(numb.begin(), numb.end(), output_iterator);

This will copy everything in numb to your output file.
Try outputting to the file, within the for loop, just like how you printed every even number.
Dec 16, 2016 at 3:52am
closed account (48T7M4Gy)
Good point, copy_if only creates a new filtered vector and of course doesn't output to a file. No real advantage by making a new vector in this case.
Dec 16, 2016 at 4:34am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>

std::ostream& copy_even( const std::vector<int>& vec, std::ostream& stm )
{
    // http://www.stroustrup.com/C++11FAQ.html#lambda
    static const auto is_even = [] ( int v ) { return v%2 == 0 ; } ;

    std::copy_if( vec.begin(), vec.end(), std::ostream_iterator<int>( stm, " " ), is_even ) ;
    return stm ;
}

int main()
{
    const std::vector<int> numbers { 0, 5, 8, 2, 3, 7, 6, 4, 9, 2, 5, 6, 7, 4, 4, 7, 2, 5 } ;

    copy_even( numbers, std::cout ) << '\n' ; // copy even numbers to stdout

    std::ofstream file( "even.txt" ) ;
    copy_even( numbers, file << "the numbers written to file are: " ) << '\n' ; // copy even numbers to file
}

http://coliru.stacked-crooked.com/a/c8f59d756cc2fa66
Dec 16, 2016 at 4:47am
closed account (48T7M4Gy)
I guess now the second point I can acknowledge is <algorithm> functionality (combined with IO stream's) is extremely powerful.
Topic archived. No new replies allowed.