← Back to Streams
Question 604
Save a Person
Save a Person
Write a functionvoid savePerson(const string& filename, const string& name, int age)that opensfilenamewith anofstream(the starter does this asout) and writes two lines:Name:followed by a space and the name, thenAge:followed by a space and the age. Each line ends with a newline. ForsavePerson("person_a.txt", "Bob", 30)the file must contain exactly (see Expected Output). The test reopens the file and prints every line, so that is also the output. Do NOT write a main function.
Expected Output:
Name: Bob Age: 30 Name: Ann Age: 7
= newline. The output ends with a newline — print one after the last line.
Hints2 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.