CJCoding With Joseph
15free / daySign inCreate account

Count Lines

A text file contains one item per line, for example:

red
green
blue

Write a function int countLines(const string& filename) that returns how many lines the file has. The starter opens the file and already loops with while (getline(in, line)); add the line inside the loop that increments count. 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
Topics:
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.