How can I add intergers from a file and assign the sum to a variable in this program

Hello, I was wondering if anyone could help me with a small problem that I have. I am trying to write a code that adds the integers from a file, but I just don't know the formula to do so. The integers in the file are ordered from 1 ,2 ,3 ,4, 5... all the way to 100.

I have looked countless times in this forum for an answer to this small problem (and I'm sure there is) but there hasn't been a post that helped me understand. If anyone could help me I would truly appreciate it.


The code below outputs the content of the file to a string and now all I want to do is find the total sum of the integers(in the file) and assign the sum to a variable so i can output it.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>

using namespace std;

int main() {

    int a,b,c;

    fstream file;
    file.open("numbers.txt");

    if (file.is_open())
    {
        file >> a >> b >> c;

        cout << a << b << c;


    }
	return 0;





}
Last edited on
Topic archived. No new replies allowed.