Calculating Standard Deviation in C++

I have a csv file with three numbers, e.g.

45
65
76

I am using the below program to calculate various statistics in the file, one being standard deviation. The code is executing, but it does not calculate standard deviation correctly. Can someone please modify the code to make it calculate standard deviation correctly?

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm> // std::max
#include <string>
using namespace std;

int main() {
ifstream infile("myfile11072014.csv");
string Title;
string XVariable;
getline(infile, Title);
getline(infile, XVariable);
float num;
float total = 0.0f;
unsigned int count = 0;



float sumDiffSqr;
float stdDev;

float sum = 0, max = 0, min = 0;

// While infile successfully extracted numbers from the stream
while (infile >> num) {
total += num;
++count;

sum += num;
if (num > max) max = num;
if (num < min) min = num;

}
// don't need the file anymore, close it
infile.close();

// test to see if anything was read (prevent divide by 0)
if (!count) {
std::cerr << "Couldn't read any numbers!" << std::endl;
return 1;
}

// give the average




sumDiffSqr = 0.;

for (num = 0; num < count; num++)
{
sumDiffSqr = sumDiffSqr + pow((num - (total / count)), 2);
}

stdDev = sqrt(sumDiffSqr/count);

std::cout << "Number of variables: " << count << std::endl;
std::cout << "The average is: " << total / count << std::endl;
std::cout << "The maximum is: " << max << std::endl;
std::cout << "The minimum is: " << min << std::endl;
std::cout << "The sum is: " << sum << std::endl;
std::cout << "The standard deviation is: " << stdDev << std::endl;
std::cout << "sumDiffSqr: " << sumDiffSqr << std::endl;
std::cout << "result of pow: " << pow((num - (total / count)), 2) << std::endl;
std::cout << "" << std::endl;
std::cout << "Press any key to continue... " << std::endl;



// pause the console
std::cin.sync();
std::cin.get();
getchar();
return 0;
}
looks like a continuation of http://www.cplusplus.com/forum/general/137956/#msg731789

please use code tags. [code]your code here[/code]
Topic archived. No new replies allowed.