for循环没有正确遍历数组.(选择3
For-loop not traversing through array correctly. (Option 3
函数"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%的全局变量应该移动到函数的局部变量
相关文章:
- 讨论 - 创建矩阵时的数组与向量的向量 - 什么是最实用的选择
- 如何从向量或数组中选择最常见的数字?(前五名)C++
- 如何为我的数组选择更大的数据类型?
- 选择一个元素而不是一个对象的数组的原因
- 如何在 c++ 中从数组中选择一个随机数?
- 从C++数组中选择一个随机元素
- 选择随机字符串数组的排序
- 我的选择排序代码是否存在导致它跳过数组中的元素的问题?
- 给定数组范围的选择排序问题
- 使用 C++ 从每个循环的数组中选择 n 个元素
- 使用STD :: String和字符数组之间的选择
- 根据 C++11 中的模板参数选择数组大小
- "我该如何展示cout<<"发烧";因为我不熟悉字符串数组,它也卡在选择部分?
- C 计数器用于从数组中选择事物
- 如何映射数组元素以选择二进制树节点
- C 选择排序字符串数组
- 在对象数组上进行选择排序
- 未选择引用到数组的构造函数进行列表初始化
- C++数组选择平方数并创建新向量
- c++数组选择