1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
andrey2020 [161]
3 years ago
7

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

t that contains:
Computers and Technology
1 answer:
Katarina [22]3 years ago
8 0

Answer:

The solution code is as follows:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7.    ifstream data;
  8.    float number;
  9.    float sum = 0;
  10.    
  11.    data.open("numbers.txt");
  12.    
  13.    while (data >> number) {
  14.        sum = sum + number;
  15.    }
  16.    cout<<sum;
  17.    return 0;
  18. }

Explanation:

Firstly, we create a ifstream object, <em>data</em> (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).

You might be interested in
PLEASE HELP ASAP
Alex73 [517]
I dont know if you need more people to answer so.. my mom, dad, brother, best friend 1, best friend 2, grandma, grandpa, cousin, aunt, uncle
7 0
3 years ago
Read 2 more answers
What is a type of machine-to-human communication?
suter [353]
I don’t know what it’s called but it’s where they can speak by moving there eyes
3 0
3 years ago
I’m joining the robotics team in my school, but my knowledge is pretty basic. What should I do to improve myself?
Aleonysh [2.5K]
Start learning the functions of a computer and really put ur heart into it
8 0
3 years ago
What is precipitation ????
kenny6666 [7]

Answer:

Precipitation has 2 meanings

In chemistry it is the action or process of precipitating a substance from a solution

But in general it is the common term for rain, snow, sleet or hail that falls to the ground

Explanation:

Just look the definition up

3 0
2 years ago
Read 2 more answers
A filing system in which an intermediary source of reference, such as a file card, must be consulted to locate specific files is
Jet001 [13]

correct me if i'm wrong and forgive me if i am wrong but it may or may not be A.

7 0
3 years ago
Read 2 more answers
Other questions:
  • What does sampling accomplish? Choose all that
    11·1 answer
  • On five lane roadways, the center lane is designated for __________ and is used by vehicles traveling in both directions.A. Thro
    15·1 answer
  • What is the color difference between the iMac and iMac Pro
    14·2 answers
  • Which is the oldest of the following sports?
    6·2 answers
  • In the RSA system, the receiver does as follows:1. Randomly select two large prime numbers p and q, which always must bekept sec
    6·1 answer
  • Bored can you talk to me
    5·2 answers
  • Write a formula that would return a TRUE result if the sum of the first five numbers in a column of data are negative
    7·1 answer
  • Which information can you apply to every page of your document with the page layout options?
    9·1 answer
  • What is output?<br> x = 2<br> y = 3<br> print (x * y + 2)<br> 4<br> 2<br> 8<br> 10
    7·1 answer
  • What is the difference between a field and an infoobject? What is the advantage of using infoobjects instead of fields in an ads
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!