Ask 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 contains:

+5
Answers (1)
  1. 4 May, 08:25
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers