鱼C论坛

 找回密码
 立即注册
查看: 2026|回复: 1

for循环中连续scanf结果错位

[复制链接]
发表于 2017-6-13 02:27:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 拽拽、圈圈 于 2017-6-14 23:12 编辑

求教各位大神 为什么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.                
  114.                
  115.                
  116.                
  117.                
  118. //        printf("姓名\t总分\t平均分\n");
  119. //       
  120. //       
  121. //       
  122. //        for (index=0; index<person; ++index) {
  123. //                //初始化总分、平均分
  124. //                total = avg = 0;
  125. //                for (key=0; key<course; ++key) {
  126. //                        total += student[index].grade[key];
  127. //                }
  128. //                printf("%s\t%d\t%d\n", student[index].name, total, (int)(total/course));
  129. //        }
  130.         return 0;
  131. }

  132. int main(void)
  133. {
  134.         Students student;
  135.         int person = 0,
  136.                 course = 0,
  137.                 choice = 0,
  138.                 index = 0;
  139.        
  140.         while((choice = showMenu())!=0){
  141.                 if (choice==1) {
  142.                         recordStudent(&student, &person, &course);
  143.                 } else if (choice==3) {
  144.                         countPerStudentCourse(&student, person, course);
  145.                 } else {
  146.                         continue;
  147.                 }
  148.         }
  149.         return 0;
  150. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-23 23:12:38 | 显示全部楼层
我觉得你是想搞个动态二维Struct,但是你的没有分配内存,也没有二维。能编译过已经很不错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 17:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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