for循环没有正确遍历数组.(选择3

For-loop not traversing through array correctly. (Option 3

本文关键字:数组 选择 遍历 循环 for      更新时间:2023-10-16

函数"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

我认为这是因为您没有在studScore()开始时将sum设置为0。

同样,你需要使用更多的函数(有更多的参数),更少的全局变量,你应该尽量避免从函数中打印东西。例如,letter()应该有一个名为int average的参数,并返回char。如果您愿意,可以创建一个单独的函数print_student_grade(),该函数接受int student参数,调用studScore()letter(),然后打印输出。

说真的,有很多函数。我发现根据它们所解决的子问题将它们分割成不同的源文件是很好的。

并且90%的全局变量应该移动到函数的局部变量