← Back to Streams
Question 577
Count Lines
Count Lines
A text file contains one item per line, for example: red green blue Write a functionint countLines(const string& filename)that returns how many lines the file has. The starter opens the file and already loops withwhile (getline(in, line)); add the line inside the loop that incrementscount. Every line counts, even an empty one. The test prints the returned number on its own line, for example (see Expected Output). Do NOT write a main function.
Expected Output:
3 4
= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
Streamsifstream
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.