学生成绩管理系统输出错误
输出平均成绩和单科成绩时第一项没问题第二项就乱码了。程序代码:
#include<iostream> #include<string> using namespace std; struct xuesheng { char xingming[30],xingbie[10]; int chengji[20]; }; void shuru(xuesheng *s,string km[],const int renshu,const int keshu) { for (int i = 0;i<renshu;i++,s++) { cout << "请输入第" << i + 1 << "个学生的信息" << endl; cout << "请输入学生姓名:"; cin >> s[i].xingming; cout << "请输入学生性别:"; cin >> s[i].xingbie; for (int j = 0; j <keshu; j++) { cout << "请输入学生的"<<km[j]<<"成绩"; cin >> s[i].chengji[j]; } cout << "第" << i + 1 << "个学生的信息输入完成" << endl; } cout << "输入结束" << endl; } void dankepaixv(xuesheng st[],string km[],const int renshu,const int keshu) { xuesheng itemp; for(int i=0;i<keshu;i++) { for(int j=0;j<renshu-1;j++) for(int k=j+1;k<renshu;k++) if(st[k].chengji[i]<st[j].chengji[i]) { itemp = st[j]; st[j] = st[k]; st[k] = itemp; } cout<<endl<<km[i]<<":"<<endl; //输出该科排好的成绩单 for(int m=0;m<renshu;m++) cout<<st[m].xingming<<" "<<st[m].chengji[i]<<endl; } } void pingjun(xuesheng st[],string km[],const int renshu,const int keshu) { int s[renshu],x; for(int i=0;i<renshu;i++) { x=0; for(int j=0;j<keshu;j++) x+=st[i].chengji[j]; s[i]=x/keshu; cout<<st[i].xingming<<" "<<s[i]<<endl; } } int main() { //输入 int r,k; cout << "请输入学生人数"; cin >> r; cout << "请输入科目数"; cin >> k; const int renshu = r; const int keshu = k; string km[keshu]; for(int i=0;i<keshu;i++) { cout << "请输入科目名称"<<i+1<<endl; cin>>km[i]; } xuesheng s[renshu]; shuru(s,km,renshu,keshu); //主菜单 while(true) {cout << "如果您需要查询单科成绩,请输入\"dk\";如果您需要查询平均成绩,请输入\"pj\""<< endl; cout << "按\"z\"返回主菜单,按\"q\"退出程序" << endl; string xuanxiang; cin >> xuanxiang; if (xuanxiang == "q") break; else if (xuanxiang =="z") continue; else if (xuanxiang =="dk") dankepaixv(s,km,renshu,keshu); else if (xuanxiang =="pj") pingjun(s,km,renshu,keshu); else cout << "输入错误,返回主菜单" << endl; } return 0; }