我想做一个程序来计算学生的平均成绩,同时降低最低的分数。为此,我为平均分数创建了3个函数,从用户输入和最低分数中获取分数。但是,在主函数中,我不是得到返回的最低值,而是只得到初始值。这是密码,谢谢。我只是张贴问题的主要部分,如果你想要其余的代码,我将编辑问题。
int main() {
int students = 0;
int avg = 0.0f;
int total = 0;
int i = 0;
float lowest = 0;
int sumofhigh = 0;
cout << "================Calculating average of grades while dropping lowest grade============\n \n";
int score[25];
getscores(score, total, i);
cout << "\n";
lowest = lowgrade(score, students);
cout << "The lowest score is: " << lowest << " ";
}
int lowgrade(int score[], int numberstudent) {
int low = score[0];
for (int i = 0; i <= numberstudent ; ++i) {
if (score[i]<low)
low = score[i];
}
return low;
}
void getscores(int score[], int &total, int i) {
int student = 0; total = 0; i = 0;;
cout << "Please enter the number of students: "; cin >> student;
while (student < 0) {
cout << "Students can't be negative can they? enter again: "; cin >> student;
}
cout << "\n";
cout << "Now enter the grades for each of the students!: \n";
for ( i = 0; i < student; ++i) {
cout << "Enter grades for student " << i + 1 << " "; cin >> score[i];
while (score[i] < 0 || score[i]>100) {
cout << "Students can't get above hundred or have negative grades, enter again: "; cin >> score[i];
}
total += score[i];
cout << "\n";
}
cout << "The total sum of grades is: " << total << " ";
}
发布于 2020-05-08 13:10:39
您正在从main调用函数lowgrade(),并将其设置为0。函数中的变量数字学生将为零。它永远不会到达for循环中,因此只接受第一个参数。
编辑:当您在getscores函数中声明学生变量时,它是一个局部变量。在函数中,您不能以这种方式更新学生的值。当您完成getscores()时,您填充了分数数组,但是学生仍然是0。
https://stackoverflow.com/questions/61688058
复制相似问题