鱼C论坛

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

for循环中连续scanf结果错位

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

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

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

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

求教各位大神 为什么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);
        }
                
                
                
                
                
//        printf("姓名\t总分\t平均分\n");
//        
//        
//        
//        for (index=0; index<person; ++index) {
//                //初始化总分、平均分
//                total = avg = 0;
//                for (key=0; key<course; ++key) {
//                        total += student[index].grade[key];
//                }
//                printf("%s\t%d\t%d\n", student[index].name, total, (int)(total/course));
//        }
        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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-29 09:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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