功能“studScore”中的for循环在输入每个学生的成绩后打印出错误的成绩单。这不是完整的代码。 选择选项1并输入学生成绩后。选择选项3并为每个学生输出相同的字母等级。
#include <iostream>
using namespace std;
int students;
double average;
int grade[300][6];
int score;
void letter();
double studScore();
double classave();
int stud;
int main()
{
int choice;
int choice1;
cout<< "Student project Database"<<endl<<endl<<endl;
do {
cout<< "Please choose an option."<<endl;
cout<< "1. To store the scores for students' quizzes. "<<endl;
cout<< "2. To compute the class average on a specific quiz."<<endl;
cout<< "3. To see the letter grade of a specific student."<<endl;
cout<< "4. To compute the overall class average in the course."<<endl;
cout<< "Press 0 to quit."<<endl;
cout<< "Please choose: ";
cin>> choice;
cout<<endl;
}
while (choice!=1);
if (choice==1){
cout<<"Please give the number of students: ";
cin>>students;
for (i =0; i<students;i++){
for (j=0; j<6; j++){
cout<<"Please give the score of student "<<(i+1)<<" in quiz "<<(j+1)<<": ";
cin>>score;
grade[i][j]=score;
}
}
cout<<endl;
do {
cout<< "Please choose an option."<<endl;
cout<< "1. To store the scores for students' quizzes. "<<endl;
cout<< "2. To compute the class average on a specific quiz."<<endl;
cout<< "3. To see the letter grade of a specific student."<<endl;
cout<< "4. To compute the overall class average in the course."<<endl;
cout<< "Press 0 to quit."<<endl;
cout<< "Please choose: ";
cin>> choice1;
cout<<endl;
if (!cin){
cin.clear();
cin.ignore();
cout<<"Invalid choice. Only options 0-4 are allowed."<<endl<<endl;
}
else if (choice1==3){
cout<<"Please give the student #: ";
studScore();
letter();
}
else if (choice1==4){
cout<<"The class average is "<<c_ave;
}
}
while(choice1!=0&&choice==1);
}
return 0;}
cout<<endl;
return average;
double studScore(){
int sum=0;
cin>>stud;
if(stud>i){
cout<<"Invalid choice for student #"<<endl<<endl;
return 0;
}
for(i=stud-1; i<=stud-1;i++){
for(j=0;j<6;j++){
sum+=grade[i][j];
ave2=(sum/6);
}
return ave2;
}
}
void letter(){
if (ave2>=93&&ave2<=100)
cout<<"The letter grade for student "<<(stud)<<" is A"<<endl<<endl;
if(ave2>=87 && ave2<93)
cout<<"The letter grade for student "<<(stud)<<" is A-"<<endl<<endl;
if(ave2>=83&&ave2<87)
cout<<"The letter grade for student "<<(stud)<<" is B+"<<endl<<endl;
if(ave2>=80&&ave2<83)
cout<<"The letter grade for student "<<(stud)<<" is B"<<endl<<endl;
if(ave2>=77&&ave2<80)
cout<<"The letter grade for student "<<(stud)<<" is B-"<<endl<<endl;
if(ave2>=73&&ave2<77)
cout<<"The letter grade for student "<<(stud)<<" is C+"<<endl<<endl;
if(ave2>=70&&ave2<73)
cout<<"The letter grade for student "<<(stud)<<" is C"<<endl<<endl;
if(ave2>=67&&ave2<70)
cout<<"The letter grade for student "<<(stud)<<" is C-"<<endl<<endl;
if(ave2<67)
cout<<"Student "<<(stud)<<" failed the course."<<endl<<endl;
}
一些示例输出
Please choose an option.
1. To store the scores for students' quizzes.
2. To compute the class average on a specific quiz.
3. To see the letter grade of a specific student.
4. To compute the overall class average in the course.
Press 0 to quit.
Please choose: 1
Please give the number of students: 3
Please give the score of student 1 in quiz 1: 95
Please give the score of student 1 in quiz 2: 95
Please give the score of student 1 in quiz 3: 95
Please give the score of student 1 in quiz 4: 95
Please give the score of student 1 in quiz 5: 95
Please give the score of student 1 in quiz 6: 95
Please give the score of student 2 in quiz 1: 80
Please give the score of student 2 in quiz 2: 80
Please give the score of student 2 in quiz 3: 80
Please give the score of student 2 in quiz 4: 80
Please give the score of student 2 in quiz 5: 80
Please give the score of student 2 in quiz 6: 80
Please give the score of student 3 in quiz 1: 56
Please give the score of student 3 in quiz 2: 56
Please give the score of student 3 in quiz 3: 56
Please give the score of student 3 in quiz 4: 56
Please give the score of student 3 in quiz 5: 56
Please give the score of student 3 in quiz 6: 56
Please choose an option.
1. To store the scores for students' quizzes.
2. To compute the class average on a specific quiz.
3. To see the letter grade of a specific student.
4. To compute the overall class average in the course.
Press 0 to quit.
Please choose: 3
Please give the student #: 2
The letter grade for student 2 is B
Please choose an option.
1. To store the scores for students' quizzes.
2. To compute the class average on a specific quiz.
3. To see the letter grade of a specific student.
4. To compute the overall class average in the course.
Press 0 to quit.
Please choose: 3
Please give the student #: 3
Invalid choice for student #
The letter grade for student 3 is B
答案 0 :(得分:0)
我认为这是因为您未在sum
开头将studScore()
设置为0。
此外,您需要使用更多函数(具有更多参数),更少的全局变量,您应该尽量避免在函数内打印内容。例如。 letter()
应该有一个名为int average
的参数,并返回char
。如果需要,可以使用名为print_student_grade()
的单独函数,该函数采用int student
参数,调用studScore()
和letter()
,然后打印输出。
说真的,很多功能。我发现根据他们解决的子问题在源文件之间进行拆分是很好的。
应将90%的全局变量移动到函数中的局部变量。