trying to extract type double from a file

I'm trying to get doubles from a file its for a more extensive homework, but my method isn't working, any suggestions?

edit: The reason why I say it isnt working is because Avg comes out : nan(not a number)

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
28
29
30
31
  #include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main()
{   ifstream inData;


    double next,num = 0,c,Avg;
    int Count = 0;


    inData.open("stdDev.txt");

    while(inData >> next)
        {

        num = num + next;
        Count++;
        }

    inData.close();

    Avg = num/Count;

    cout << "Avg: " << Avg;
return 0;
}
Last edited on
Hi,

Your problem is with line 5 and your variable next. Because you have brought in the entire std namespace, your variable next is interpreted as std::next which is a very different thing.

So this is a very good reason not to have line 5, instead prefix every std thing with std::, as in: std::ifstream , std::cout. If you look at code posted here by experts like Cubbi and JLBorges and many others, they never bring in all of std and always use std::

Also, it is a good idea to declare an initialise each of your variables to something, preferably one per line.

line 20 can be written: num += next;

I hope this helps you out a bit, and you have a great day.

Regards :+)


http://en.cppreference.com/w/cpp/iterator/next
Last edited on
Topic archived. No new replies allowed.