我在C++項目工作「不在範圍內聲明」,並在編譯時,我收到錯誤消息:收到錯誤消息
error:
mean
was not declared in this scope
error:standard_dev
was not declared in this scope
我的代碼是
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int N(0);
char filename[100];
double m, stdev;
string temp;
double next;
int count = 0;
cout << "Enter name of file: ";
cin >> filename;
ifstream myfile;
myfile.open(filename);
while (myfile >> next)
{
count++;
}
N = count;
double* mydata;
mydata = new double[N];
for (int i = 0; i < N; i++)
{
myfile >> mydata[i];
}
m = mean(mydata, N);
stdev = standard_dev(mydata, m, N);
cout << "The standard deviation is:" << stdev << endl;
myfile.close();
delete[] mydata;
return 0;
}
double mean(double* mydata, double N)
{
double sum(0), m;
for (int i = 0; i < N; i++)
{
sum += mydata[i];
}
m = (sum/(double) N);
return (m);
}
double standard_dev(double* mydata, double m, int N)
{
double* mydata2 = new double[N];
for (int i = 0; i < N; i++)
{
mydata2[i] = pow((mydata[i] - m), 2);
}
double sum(0), S, X;
for (int i = 0; i < N; i++)
{
sum += mydata2[i];
}
X = sum/N;
S = sqrt(X);
return (S);
}