鱼C论坛

 找回密码
 立即注册
查看: 2258|回复: 5

[已解决]for循环中连续scanf结果错位

[复制链接]
发表于 2017-6-18 23:05:44 | 显示全部楼层 |阅读模式
100鱼币
本帖最后由 拽拽、圈圈 于 2017-6-18 23:06 编辑

求教各位大神 为什么3选项的姓名打印不正确
环境是dev-c++ TDM-GCC 4.9.2

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_STUDENT 30
  5. #define MAX_COURSE 6

  6. typedef struct{
  7.         //学号
  8.         int Id;
  9.         //学生名称长度
  10.         char name[20];
  11.         //课程分数
  12.         int grade[MAX_COURSE];
  13.         //课程总分
  14.         int score;
  15.         //平均分
  16.         int average;
  17. } Students;

  18. //使用的函数

  19. //打印菜单
  20. int showMenu(void);

  21. //录入成绩 菜单 1
  22. Students recordStudent(Students * student, int *person, int *course);

  23. //计算每门课程总分及平均分 菜单 2
  24. int countPerCourse(const Students *student, int person, int course);

  25. //计算每个学生的总分和平均分 菜单 3
  26. int countPerStudentCourse(const Students *student, int person, int course);

  27. //按学生总分由高到低排名 菜单 4
  28. int sortByTotalScore(const Students *student, int person, int course);

  29. //打印成绩表
  30. int printScoreList(const Students *student, int person, int course);

  31. /**
  32. * 显示操作目录
  33. * @return int 需要执行的操作
  34. */
  35. int showMenu()
  36. {
  37.         int choice = 0;
  38.         printf("====考试系统 v1.0====\n"
  39.                 "操作选项:\n"
  40.                 "\t1.录入每个学生的学号、姓名和各科考试成绩\n"
  41.                 "\t2.计算每门课程的总分和平均分\n"
  42.                 "\t3.计算每个学生的总分和平均分\n"
  43.                 "\t4.按每个学生的总分由高到低排出名次表\n"
  44.                 "\t5.按学号由小到大排出成绩表\n"
  45.                 "\t6.按姓名的字典顺序排出成绩表\n"
  46.                 "\t7.按学号查询学生排名及考试成绩\n"
  47.                 "\t8.按姓名查询学生排名及考试成绩\n"
  48.                 "\t9.统计每门课程每个类别(优秀,良好,中等,及格,不及格)的人数及比例\n"
  49.                 "\t10.打印学生的学号、姓名、各科考试成绩以及每门课程的总分和平均分\n"
  50.                 "\t0.退出\n"
  51.                 "请选择:"
  52.         );
  53.         scanf("%d", &choice);
  54.         return choice;
  55. }

  56. /**
  57. * 录入学生成绩 菜单 1
  58. * @param int person 人数
  59. * @param int course 课程数
  60. * @return int
  61. */
  62. Students recordStudent(Students * student, int *person, int *course)
  63. {
  64.         int index,
  65.                 _person = 0,
  66.                 _course = 0,
  67.                 key;
  68.         
  69.         printf("请输入学生人数:");
  70.         scanf("%d", &_person);
  71.         
  72.         printf("考试课程数:");
  73.         scanf("%d", &_course);
  74.         
  75.         
  76.         //通过指针修改全局学生人数,课程数
  77.         *person = _person;
  78.         *course = _course;
  79.         
  80.         printf("====开始录入学生信息====\n");
  81.         
  82.         for (index=0; index<_person; ++index) {
  83.                 printf("学号:");
  84.                 scanf("%d", &student[index].Id);
  85.                
  86.                 printf("姓名:");
  87.                 scanf("%s", student[index].name);

  88.                 printf("分数:\n");
  89.                 for (key=0; key<_course; ++key) {
  90.                         printf("  第%d门:", key+1);
  91.                         scanf("%d", &student[index].grade[key]);
  92.                 }
  93.                 printf("\n");
  94.         }
  95. }

  96. /**
  97. * 计算每个学生的总分和平均分 菜单 3
  98. * @param Students student 学生信息
  99. * @param int person 学生人数
  100. * @param int course 课程数
  101. * @return int
  102. */
  103. int countPerStudentCourse(const Students *student, int person, int course)
  104. {
  105.         int index =0,
  106.                 key = 0,
  107.                 total = 0,
  108.                 avg = 0;
  109.         for (index=0; index<person; ++index) {
  110.                
  111.                 printf("%s\n", student[index].name);
  112.         }
  113.         return 0;
  114. }

  115. int main(void)
  116. {
  117.         Students student;
  118.         int person = 0,
  119.                 course = 0,
  120.                 choice = 0,
  121.                 index = 0;
  122.         
  123.         while((choice = showMenu())!=0){
  124.                 if (choice==1) {
  125.                         recordStudent(&student, &person, &course);
  126.                 } else if (choice==3) {
  127.                         countPerStudentCourse(&student, person, course);
  128.                 } else {
  129.                         continue;
  130.                 }
  131.         }
  132.         return 0;
  133. }
复制代码
最佳答案
2017-6-18 23:05:45
本帖最后由 ba21 于 2017-6-18 23:40 编辑

  for (index=0; index<_person; index++) {

for (index=0; index<person; index++) {

  for (key=0; key<_course; key++) {
未标题-1.png

最佳答案

查看完整内容

for (index=0; index
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-18 23:05:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ba21 于 2017-6-18 23:40 编辑

  for (index=0; index<_person; index++) {

for (index=0; index<person; index++) {

  for (key=0; key<_course; key++) {
未标题-1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-6-19 21:16:37 | 显示全部楼层
ba21 发表于 2017-6-18 23:27
for (index=0; index

嵌套循环是修改的哪里呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-19 21:19:49 | 显示全部楼层
拽拽、圈圈 发表于 2017-6-19 21:16
嵌套循环是修改的哪里呢

你上面总共就3处++。你这3处都是在前面,改成到后面就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-20 11:20:11 | 显示全部楼层
还没有解决吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-23 23:35:14 | 显示全部楼层
?????????????怎么还没解决?这币是挣不到了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 05:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表