Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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++_Function_Struct - Fatal编程技术网

C++ 将结构数组中的某个条目传递到函数中

C++ 将结构数组中的某个条目传递到函数中,c++,function,struct,C++,Function,Struct,我的结构看起来像: struct Student { string sStudentFName; string sStudentLName; int iTestScore; char cGrade; }; 然后我创建了一个名为Students的数组,它包含20个Student结构 Student Students[20] = {}; 然后我创建了这个函数来打印每个学生的信息: void PrintStudentInfo(Student students[],

我的结构看起来像:

struct Student
{
    string sStudentFName;
    string sStudentLName;
    int iTestScore;
    char cGrade;
};
然后我创建了一个名为Students的数组,它包含20个Student结构

Student Students[20] = {};
然后我创建了这个函数来打印每个学生的信息:

void PrintStudentInfo(Student students[], const int iSize)
{
    /// Get the longest student name (first and last).
    static size_t iLongestFullName = 0;
    for (int i = 0; i < iSize; i++)
    {
        string sFullName = students[i].sStudentFName + ", " + students[i].sStudentLName;
        if (iLongestFullName < sFullName.length())
            iLongestFullName = sFullName.length();
    }

    /// Print the scores....
    for (int i = 0; i < iSize; i++)
    {
        string sFullName = students[i].sStudentFName + ", " + students[i].sStudentLName;
        cout << left << setfill('.') << setw(16);
        cout << sFullName;
        cout << students[i].iTestScore;
        cout << right << setfill('.') << setw(8);
        cout << students[i].cGrade;
        cout << endl;
    }
}
这一行将打印出90和100范围内每个学生的信息

我无法理解将特定条目从students数组传递到PrintStudentInfo函数的语法。我必须调用PrintStudentInfo范围内的PrintStudentInfo函数 我得到的错误是*不存在从“学生”到“学生”的适当转换函数


提前谢谢你的帮助

PrintStudentInfo
需要一个指向
Student
的指针,以及从该指针开始打印的
Student
的数量

因此,您可以将正确的指针传递给第一个学生,然后调整大小
1

PrintStudentInfo(&students[i], 1);

但是,从代码质量的角度来看,我鼓励您编写一个只打印一个学生的函数,然后在循环中由
printstudentfo
调用该函数以打印所有学生,并且可以被
printstudentforage
重用


我还建议不要使用原始数组,因为它们的行为并不直观。改为使用
std::vector

您正在PrintStudentInfo()和PrintStudentInfo()中循环所有学生。派了一个学生来印刷。在下一个级别中,尝试使用std::vector代替数组:

void PrintStudentInfo(Student* pstudents)
{
        string sFullName = pstudents->sStudentFName + ", " + pstudents->sStudentLName;
        cout << left << setfill('.') << setw(16);
        cout << sFullName;
        cout << pstudents->iTestScore;
        cout << right << setfill('.') << setw(8);
        cout << pstudents->cGrade;
        cout << endl;
}


void PrintStudentInfoRange(const int iMinScore, const int iMaxScore, Student students[], const int iSize)
{
    for (int i = 0; i < iSize; i++) {
        if (students[i].iTestScore >= iMinScore && students[i].iTestScore <= iMaxScore) {
            PrintStudentInfo(&students[i]);
        }
    }
}
无效打印学生信息(学生*pstudents)
{
字符串sFullName=pstudents->sStudentFName+,“+pstudents->sStudentLName;

cout
PrintStudentInfo
接收一个学生列表[],因此传入一个(例如
Student
)是不可能的,除非它是一个包含一个学生的数组。编写一个新的
PrintOneStudentInfo(…)
函数,该函数接收一个学生并将学生的信息打印出来。。。。(然后可以重构
PrintStudentInfo
函数,对每个学生调用
PrintOneStudentInfo(..)
的数组进行迭代)。一个函数建立在另一个函数的基础上。…@alexyorke因为他们使用原始数组/指针,所以从技术上讲,他们可以使用它只打印一个
学生
。不过,我也会说应该有一个函数打印一个学生。这是个好主意。不幸的是,作业只告诉我修改PrintStudentInf中的内容oRange和我无法创建任何新函数。感谢您的帮助alexyorke和David C.RankinI试图将指针传递给第一个学生,但我遇到了以下错误:
terminate在抛出'std::bad_alloc'what():std::bad_alloc中止(内核转储)实例后调用
所以我认为这不是解决方案。哦,等一下,我道歉。它成功了。我输入了20而不是1。我理解为什么会发生错误。感谢您的帮助和其他解决方案。
PrintStudentInfo(&students[i], 1);
void PrintStudentInfo(Student* pstudents)
{
        string sFullName = pstudents->sStudentFName + ", " + pstudents->sStudentLName;
        cout << left << setfill('.') << setw(16);
        cout << sFullName;
        cout << pstudents->iTestScore;
        cout << right << setfill('.') << setw(8);
        cout << pstudents->cGrade;
        cout << endl;
}


void PrintStudentInfoRange(const int iMinScore, const int iMaxScore, Student students[], const int iSize)
{
    for (int i = 0; i < iSize; i++) {
        if (students[i].iTestScore >= iMinScore && students[i].iTestScore <= iMaxScore) {
            PrintStudentInfo(&students[i]);
        }
    }
}