Ask Question
9 December, 02:02

Write a program sum. cpp that reads a sequence of integers from cin, and reports their sum. Example If you have a file numbers. txt that contains:

+5
Answers (1)
  1. 9 December, 05:52
    0
    The solution code is as follows:

    #include #include #include using namespace std; int main () { ifstream data; float number; float sum = 0; data. open ("numbers. txt"); while (data >> number) { sum = sum + number; } cout<
    Explanation:

    Firstly, we create a ifstream object, data (Line 9). Then we can use the ifstream object to open the "numbers. txt" (Line 13) and use while loop to traverse through the number in the text file line by line (Line 15). White the loop is ongoing the number in each line is read by >> operator in a similar way to cin and then add each read number to sum variable (Line 16).

    At last, display the output (Line 18).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program sum. cpp that reads a sequence of integers from cin, and reports their sum. Example If you have a file numbers. txt that ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers