For 循环未正确遍历数组。 (选项 3

Posted

技术标签:

【中文标题】For 循环未正确遍历数组。 (选项 3【英文标题】:For-loop not traversing through array correctly. (Option 3 【发布时间】:2014-03-13 02:54:03 【问题描述】:

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

【问题讨论】:

你知道如何使用调试器 你能展示一些示例输出吗? 不,这是我的第一个学期编码。 我在原帖中放了一些样本 下次最好不要在代码中使用全局变量。 【参考方案1】:

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

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

说真的,很多功能。而且我发现最好根据它们解决的子问题在源文件之间拆分它们。

90% 的全局变量应该移动到函数中的局部变量中。

【讨论】:

while(choice1!=0&&choice==1) 这个条件有什么作用?

以上是关于For 循环未正确遍历数组。 (选项 3的主要内容,如果未能解决你的问题,请参考以下文章

jquery 如何遍历循环数组

JS数组循环遍历常用的9种方法

for循环二维数组的取值方式?

遍历数组

js 循环遍历变量的几种方式

for each循环遍历数组