鱼C论坛

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

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

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

求教各位大神 为什么3选项的姓名打印不正确
环境是dev-c++ TDM-GCC 4.9.2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 30
#define MAX_COURSE 6

typedef struct{
        //学号
        int Id;
        //学生名称长度 
        char name[20];
        //课程分数
        int grade[MAX_COURSE];
        //课程总分
        int score;
        //平均分
        int average;
} Students;

//使用的函数

//打印菜单
int showMenu(void);

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

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

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

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

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

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

/**
 * 录入学生成绩 菜单 1
 * @param int person 人数
 * @param int course 课程数
 * @return int
 */
Students recordStudent(Students * student, int *person, int *course)
{
        int index,
                _person = 0,
                _course = 0,
                key;
        
        printf("请输入学生人数:"); 
        scanf("%d", &_person);
        
        printf("考试课程数:");
        scanf("%d", &_course);
        
        
        //通过指针修改全局学生人数,课程数
        *person = _person;
        *course = _course;
        
        printf("====开始录入学生信息====\n");
        
        for (index=0; index<_person; ++index) {
                printf("学号:");
                scanf("%d", &student[index].Id);
                
                printf("姓名:");
                scanf("%s", student[index].name);

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

/**
 * 计算每个学生的总分和平均分 菜单 3
 * @param Students student 学生信息
 * @param int person 学生人数
 * @param int course 课程数
 * @return int
 */
int countPerStudentCourse(const Students *student, int person, int course)
{
        int index =0,
                key = 0,
                total = 0,
                avg = 0;
        for (index=0; index<person; ++index) {
                
                printf("%s\n", student[index].name);
        }
        return 0;
}

int main(void)
{
        Students student;
        int person = 0,
                course = 0,
                choice = 0,
                index = 0;
        
        while((choice = showMenu())!=0){
                if (choice==1) {
                        recordStudent(&student, &person, &course);
                } else if (choice==3) {
                        countPerStudentCourse(&student, person, course);
                } else {
                        continue;
                }
        }
        return 0;
}
最佳答案
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-9-29 09:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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