萌新sjsndh 2022-02-26 15:27 采纳率: 100%
浏览 43
已结题

输出一行的时候没问题,输出两三行就有乱码,这怎么解决(语言-c语言)

问题遇到的现象和发生背景

输出一行的时候没问题,输出两三行就有乱码,这怎么解决

img

img

img

问题相关代码,请勿粘贴截图

#include<stdio.h>
#include<stdlib.h>
struct course *head1;
struct  course{
    int bianhao;//编号 
    char mingcheng[20];// 名称 
    char xingzhi[20];//性质
    int xueshi;//学时 
    int xueqi;//学期 
    int zongrenshu;//总人数 
    int yixuanrenshu; //已选人数 
    char jiaoshi[20];
    char leixing[20];
    struct course * next;
};
  int main(){
   
    int a;
    struct course *p,*p1;
    p=(struct course*)malloc(sizeof(struct course));
    printf("课程性质中,分为文科类和理科类\n类型中,分为必修、限选、实践、实习\n");
    printf("课程编号\t课程名称\t课程性质\t学时\t学期\t可选人数\t主讲教师\t类型\t\n");
    scanf("%d%s%s%d%d%d%s%s",&p->bianhao,&p->mingcheng,&p->xingzhi,&p->xueshi,&p->xueqi,&p->zongrenshu,&p->jiaoshi,&p->leixing);
    p->yixuanrenshu=0;
    p->next=NULL;
    head1=p;
    printf("是否继续录入?1代表yes,2代表no\n");
    scanf("%d",&a);
    switch(a){ 
        case 1:{
            do{
            
                p1=(struct course*)malloc(sizeof(struct course));
                p->next=p1;
                
                printf("请输入\n");
                
                scanf("%d%s%s%d%d%d%s%s",&p1->bianhao,&p1->mingcheng,&p1->xingzhi,&p1->xueshi,&p1->xueqi,&p1->zongrenshu,&p->jiaoshi,&p->leixing);
                p1->next=NULL;
                p1->yixuanrenshu=0;
                printf("是否继续录入?1代表yes,2代表no\n");
                scanf("%d",&a);
                p=p->next;
            }while(a==1);
            
             
            break;
        }
        case 2:break;
        default: break;
    }
     struct course *c;
    c=(struct course*)malloc(sizeof(struct course));
    c=head1;    printf("编号\t名称\t性质\t学时\t学期\t已选\t总人数\t教师\t类型\n");
    if(c->next==NULL){
    
           printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%s\t%s\n",c->bianhao,c->mingcheng,c->xingzhi,c->xueshi,c->xueqi,c->yixuanrenshu,c->zongrenshu,c->jiaoshi,c->leixing);
           goto end1;}
    while(c->next!=NULL){
    
           printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%s\t%s\n",c->bianhao,c->mingcheng,c->xingzhi,c->xueshi,c->xueqi,c->yixuanrenshu,c->zongrenshu,c->jiaoshi,c->leixing);
        c=c->next;
        }
        if(c->next==NULL)
              printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%s\t%s\n",c->bianhao,c->mingcheng,c->xingzhi,c->xueshi,c->xueqi,c->yixuanrenshu,c->zongrenshu,c->jiaoshi,c->leixing);
    
    end1:; 
    }

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

3条回答 默认 最新

  • 关注

    你的结构体有九个属性,输入的时候只有8个输入的控制符,少了一个,另外字符数组输入不用加取地址符
    另外你在多次输入时候最后面两个应该是p1你写的是p所以报错(你的39行)
    改之后:

    img

    
    #include<stdio.h>
    #include<stdlib.h>
    struct course *head1;
    struct  course
    {
        int bianhao;//编号
        char mingcheng[20];// 名称
        char xingzhi[20];//性质
        int xueshi;//学时
        int xueqi;//学期
        int zongrenshu;//总人数
        int yixuanrenshu; //已选人数
        char jiaoshi[20];
        char leixing[20];
        struct course * next;
    };
    int main()
    {
    
        int a;
        struct course *p,*p1;
        p=(struct course*)malloc(sizeof(struct course));
        printf("课程性质中,分为文科类和理科类\n类型中,分为必修、限选、实践、实习\n");
        printf("课程编号\t课程名称\t课程性质\t学时\t学期\t可选人数\t主讲教师\t类型\t\n");
        scanf("%d %s %s %d %d %d %s %s",&p->bianhao,p->mingcheng,p->xingzhi,&p->xueshi,&p->xueqi,&p->zongrenshu,p->jiaoshi,p->leixing);
        p->yixuanrenshu=0;
        p->next=NULL;
        head1=p;
        printf("是否继续录入?1代表yes,2代表no\n");
        scanf("%d",&a);
        switch(a)
        {
        case 1:
        {
            do
            {
                p1=(struct course*)malloc(sizeof(struct course));
                p->next=p1;
                printf("请输入\n");
                scanf("%d %s %s %d %d %d %s %s",&p1->bianhao,p1->mingcheng,p1->xingzhi,&p1->xueshi,&p1->xueqi,&p1->zongrenshu,p1->jiaoshi,p->leixing);
                p1->next=NULL;
                p1->yixuanrenshu=0;
                printf("是否继续录入?1代表yes,2代表no\n");
                scanf("%d",&a);
               // p=p->next;
            }
            while(a==1);
            break;
        }
        case 2:
            break;
        default:
            break;
        }
        printf("编号\t名称\t性质\t学时\t学期\t已选\t总人数\t教师\t类型\n");
        while(p!=NULL)
        {
            printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%s\t%s\n",p->bianhao,p->mingcheng,p->xingzhi,p->xueshi,p->xueqi,p->yixuanrenshu,p->zongrenshu,p->jiaoshi,p->leixing);
            p=p->next;
        }
    }
    /* 1 1 1 1 1 1 1 1
       2 2 2 2 2 2 2 2
    */
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月6日
  • 已采纳回答 2月26日
  • 创建了问题 2月26日

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!