我的结构看起来像:
struct Student
{
string sStudentFName;
string sStudentLName;
int iTestScore;
char cGrade;
};
然后,我创建了一个名为“学生”的数组,其中包含20个学生结构
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;
}
}
我想创建一个函数,该函数将打印出每个学生的信息,但前提是它们必须在iMinScore和iMaxScore之间。
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) {
// issue occurs here \/
PrintStudentInfo(students[i], iSize);
}
}
}
我通过以下方式调用PrintStudentInfoRange函数:
PrintStudentInfoRange(90, 100, Students, STUDENTS);
此行将打印出每个学生的信息(介于90和100之间)。
我无法弄清楚将特定条目从students数组传递到PrintStudentInfo函数的语法。我打电话给PrintStudentInfo内功能PrintStudentInfoRange我得到的错误是*从“学生”到“学生没有合适的转换功能”的存在。
预先感谢您的帮助。
PrintStudentInfo
需要一个指针,Student
并Student
从该指针开始打印s的数量。
因此,您可以做的是将正确的指针传递给第一个学生,然后传递size 1
:
PrintStudentInfo(&students[i], 1);
但是从代码质量的角度来看,我建议您编写一个仅打印一个学生的函数,然后PrintStudentInfo
在循环中调用,以打印所有学生,并且可以将其重用PrintStudentInfoRange
。
我也建议不要使用原始数组,因为它们的行为不直观。使用std::vector
代替。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句