Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 动态分配结构_C++_Structure_Dynamic Allocation - Fatal编程技术网

C++ 动态分配结构

C++ 动态分配结构,c++,structure,dynamic-allocation,C++,Structure,Dynamic Allocation,所以我在这里遇到了麻烦。当我为numStudents输入1时,程序运行得非常好,但当我为numStudents输入1时,会出现分段错误:11。我的动态分配有什么问题吗?我只是迷路了,我已经做了我能想到的一切 #include <iostream> #include <string> #include <cstdlib> #include <iomanip> using namespace std; //Structure Declaration

所以我在这里遇到了麻烦。当我为numStudents输入1时,程序运行得非常好,但当我为numStudents输入1时,会出现分段错误:11。我的动态分配有什么问题吗?我只是迷路了,我已经做了我能想到的一切

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

//Structure Declaration
struct Student
{
    string name;
    long long ID;
    double *score;
};

//Function prototypes
void calcAvg (int loc, Student test[], double average[], int tests);

int main()
{
    int numStudents, numTests;  //Get from user
    double  *averages;  //For Dynamic allocation of averages
    Student *info;  //For Dynamic Allocation

    cout << "Enter the number of students you will enter ";
    cin >> numStudents;

    info = new Student[numStudents];
    averages = new double[numStudents];

    cout << "\nEnter the number of tests that were taken by the students ";
    cin >> numTests;

    info->score = new double[numTests];

    for(int s = 0; s < numStudents; s++)
    {
        cout << "Enter student #" << (s+1) << "'s name ";
        cin.ignore();
        getline(cin, info[s].name);
        cout << "Enter " << info[s].name << "'s ID number ";
        cin >> info[s].ID;
        cout << endl;

        for(int t = 0; t < numTests; t++)
        {
            cout << "\nEnter " << info[s].name << "'s score for test #" <<(t+1) << " ";
            cin >> info[s].score[t];

            while(info[s].score[t] > 100 || info[s].score[t] < 0)
            {
                cout << "The score you entered is invalid, try again. ";
                cin >> info[s].score[t];
            }
        }

    calcAvg(s, info, averages, numTests);
    }

    return 0;
}


void calcAvg (int loc, Student test[], double average[], int tests)
{
    double total = 0;

    for(int i = 0; i < tests; i++)
    {
        total += test[loc].score[i];
    }
    average[loc] = total/tests;

    cout << average[loc] << endl;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//结构声明
体类型
{
字符串名;
长ID;
双倍*分;
};
//功能原型
无效calcAvg(国际loc,学生测试[],双倍平均[],国际测试);
int main()
{
int numStudents,numTests;//从用户获取
双*平均值;//用于动态分配平均值
学生*info;//用于动态分配
女学生;
info=新学生[numStudents];
平均数=新的双倍[学生人数];
cout>numTests;
信息->分数=新的双倍[numTests];
对于(int s=0;s你需要为每个学生重复这个吗

info->score = new double[numTests];
因此,您可以将其移动到循环中:

for(int s = 0; s < numStudents; s++)
{
   info[s].score = new double[numTests];
   ...
}
for(int s=0;s

但是所有这些都是非常容易出错的-我建议您研究能够为您处理所有这些的结构,就像
std::vector

您需要为每个学生重复这些

info->score = new double[numTests];
因此,您可以将其移动到循环中:

for(int s = 0; s < numStudents; s++)
{
   info[s].score = new double[numTests];
   ...
}
for(int s=0;s

但是所有这些都是非常容易出错的-我建议您研究能够为您处理所有这些的结构,就像
std::vector

您需要为每个学生重复这些

info->score = new double[numTests];
因此,您可以将其移动到循环中:

for(int s = 0; s < numStudents; s++)
{
   info[s].score = new double[numTests];
   ...
}
for(int s=0;s

但是所有这些都是非常容易出错的-我建议您研究能够为您处理所有这些的结构,就像
std::vector

您需要为每个学生重复这些

info->score = new double[numTests];
因此,您可以将其移动到循环中:

for(int s = 0; s < numStudents; s++)
{
   info[s].score = new double[numTests];
   ...
}
for(int s=0;s


但所有这些都是非常容易出错的-我建议你研究一下可以为你处理所有这些的结构,比如
std::vector

,它不起作用,仍然有相同的分段错误。这是一个类,我们不应该使用像std::vector这样的东西。我也有其他人研究过,他们没有我看不出有什么不对劲,我也不……所以不sure@JordanMcPeek:这个答案不仅有意义,而且我刚刚测试了它,它是有效的。使用您的原始代码,如果输入的学生超过1名,我会得到一个SEGFULT。删除您的
info->score=new double[numTests];
并添加
info[s]。score=new double[numTests]
到循环的顶端,它成功地运行到最后。因此…如果它对你不起作用,你几乎肯定做错了什么。发布你正在测试的实际代码以及失败的地方。哎呀,我想我意识到我做了什么,我只是将info->score复制到我的循环中,没有放入info.score。使用Michael Anderson的修复程序,它对我来说很好(见3名学生).我觉得这个设计有点可笑的是,平均数与结构分开,虽然它只是每个学生的平均数,而不是一个全局平均数。但也许你必须这样做,因为你的作业中的一些细节我们不知道。非常感谢你们的帮助,在修复之后,我能够完成我的程序是的。这不起作用,仍然有相同的分段错误。这是一个类,我们不应该使用像std::vector这样的东西,我相信。我也让其他人看了这个,他们没有看到任何错误,我也没有…所以没有sure@JordanMcPeek当前位置这个答案不仅有意义,而且我刚刚测试了它,一个d有效。对于您的原始代码,如果输入的学生超过1名,我会得到一个SEGFULT。删除您的
info->score=new double[numTests];
并添加
info[s]。score=new double[numTests]
到循环的顶端,它成功地运行到最后。因此…如果它对你不起作用,你几乎肯定做错了什么。发布你正在测试的实际代码以及失败的地方。哎呀,我想我意识到我做了什么,我只是将info->score复制到我的循环中,没有放入info.score。使用Michael Anderson的修复程序,它对我来说很好(见3名学生).我觉得这个设计有点可笑的是,平均数与结构分开,虽然它只是每个学生的平均数,而不是一个全局平均数。但也许你必须这样做,因为你的作业中的一些细节我们不知道。非常感谢你们的帮助,在修复之后,我能够完成我的程序是的。这不起作用,仍然有相同的分段错误。这是一个类,我们不应该使用像std::vector这样的东西,我相信。我也让其他人看了这个,他们没有看到任何错误,我也没有…所以没有sure@JordanMcPeek当前位置这个答案不仅有意义,而且我刚刚测试了它,一个d有效。对于您的原始代码,如果输入的学生超过1名,我会得到一个SEGFULT。删除您的
info->score=new double[numTests];
并添加
info[s]。score=new double[numTests]
到循环的顶端,它成功地运行到最后。因此…如果它对你不起作用,你几乎肯定做错了什么。发布你正在测试的实际代码以及失败的地方。哎呀,我想我意识到我做了什么,我只是将info->score复制到我的循环中,没有放入info.score。使用Michael Anderson的修复程序,它对我来说很好(见3名学生)。我发现这个设计有一点有趣的是,平均值与结构分开,尽管它只是每个学生的平均值,而不是全局平均值。但也许你不得不这样做,因为有些细节