← Back to Streams
Question 609
Write Only Evens
Write Only Evens
Write a functionint writeEvens(const string& filename, const vector<int>& v)that opensfilenamewith anofstreamand writes only the even values ofv, in their original order, on a single line separated by single spaces, with no space before the first value, followed by one newline. It returns how many values it wrote and prints nothing itself. Every test vector contains at least one even number. Forv = {1, 4, 3, 6}the test prints the returned count, then reopens the file and prints every line (see Expected Output). Do NOT write a main function.
Expected Output:
2 4 6 3 2 8 10
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Streamsofstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.