# C++ help

This topic is 2683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need to take inputs from a data.dat file and compute the average and standard deviation but I'm having trouble getting it to calculate correctly.
#include <iostream>#include <fstream> #include <cstdlib>#include <cmath>using namespace std;int main(){ ifstream fin; fin.open("data_test.dat");if (fin.fail())  {    cout<<"fail to open  \n";    exit(1);  }char num;double average_num = 0;double average;double std;double holder;while (!fin.eof()){  ++average_num;  holder += num;  fin.get(num);}average = holder / average_num;  fin.close();  fin.clear();  fin.open("data_test.dat");while (!fin.eof()){  holder += pow((num + average), 2);  fin.get(num);}std = sqrt(average);  fin.close();  fin.clear();  fin.open("data_test.dat");cout << "Average = " << average << endl;cout << "Standard Deviation = " << std << endl;return(0);}
This is what I have so far. The numbers that I'm testing with are:
3.800.801.301.500.102.701.00

and the output SHOULD be:
Average = 1.6000Standard deviation = 1.1588

but for some reason its giving me 40.2222 and 6.3421. If you could help me out and tell me where I went wrong I'd greatly appreciate it.

##### Share on other sites
To add on top of what SiCrane has said, what does holder do? You assign values to it but that's it. You just assign values to it. It has no use except accepting those values.

As SiCrane mentioned, you're not initializing your values and you're getting random junk assigned to them, therefore, you get improper answers. You get 40.2 for average and I got 1.37 :D

##### Share on other sites
I got a proj4.cc:51: error: invalid use of member (did you forget the `&' ?) reply from my compiler when I changed fin.get(num); to fin.get >> num; I also set holder to 0.

##### Share on other sites
boogyman would it be possible for you to post your working code? And for you to also explain it to me? If its not too much to ask for. I'm more of a visual learner.

##### Share on other sites
It's just fin >> num; Remember to change the type of the variable as well.

##### Share on other sites
Ok it works now. Thank you. :D