Sum Numbers in a File
easyQuestion 578A text file contains whole numbers separated by spaces or newlines, for example:
10 20 30
Write a function int sumFile(const string& filename) that returns the sum of every number in the file. The starter opens the file and already loops with while (in >> n); add the line inside the loop that adds n to total. Numbers may be negative and several may share one line.
The test prints the returned sum on its own line, for example (see Expected Output).
Do NOT write a main function.
Expected output
60 11
= newline. The output ends with a newline, so 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.