【发布时间】:2018-04-30 16:09:37
【问题描述】:
我正在尝试在这里创建一个大小为“i”的数组,其中 i 是先前定义的,(底部有完整代码)
double studentScores[] = new double[i];
但是我不断收到以下错误:
需要使用“{...}”进行初始化。
我已经尝试过指针方法,但它似乎不适用于我的其余代码。任何帮助将不胜感激,感谢您的宝贵时间。
int main()
{
ifstream inData; //input file stream variable
ofstream outData; //output file stream variable
inData.open("Data.txt"); //open data file
outData.open("testStatistics.out");
int i = 0;
while (inData.eof() == false) //while you have not reached the end of the file
{
i++; //i == size of the class
}
double studentScores[] = new double[i]; //creates an array of the size of the number of inputs
for (int j = 0; j < i; j++)
{
inData >> studentScores[j]; //read in student scores
}
double average1 = average(i, studentScores);
double median1 = median(i, studentScores);
int distribution[10] = { 0 };
for (int v = 0; v < i; v++) //increment distribution appropriately
{
int h = scoresDistribution(v, studentScores);
distribution[h] ++;
}
outData << "There are " << i << "scores available." << endl;
outData << "The average is : " << average1 << endl;
outData << "The median is : " << median1 << endl;
outData << "The detailed grade distribution is as follows : " << endl;
outData << fixed << left;
outData << setfill(' ') << setw(10) << "range" << setw(10) << " # of Students" << endl;
int z = 100;
int y = 90;
for (int f = 0; f < 10; f++)
{
outData << setfill(' ');
outData << setw(10) << "[" << z << " - " << y << "]";
outData << distribution[f] << endl;
z = z - 10;
y = y - 10;
}
inData.close(); //close input data file
outData.close(); //close output data file
cout << "Press any key to quit…" << endl;
cin.ignore(50, '\n');
return 0;
}
【问题讨论】:
标签: c++ arrays compiler-errors initialization