Doing Math with file inports

The file "file1.txt" contains 0.0 0.1 0.2 0.5 0.8 0.9 1.0
I know very little about file I/O and I really need help figuring out how to take each of the numbers in there and add them up into a float variable.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()

{
	
	string data1;
	ifstream myfile ("file1.txt");

	getline (myfile, data1);
	cout << data1 << endl;
	

	myfile.close();

	return 0;

}


thanks in advance!
1
2
3
4
5
6
7
8
9
    double total = 0;
    double n;

    ifstream myfile ("file1.txt");
    
    while (myfile >> n)
        total += n;

    cout << total;
Topic archived. No new replies allowed.