Ask Question
1 June, 16:36

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. 1 June, 19:49
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers