输出一行的时候没问题,输出两三行就有乱码,这怎么解决(语言-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:; 
    }

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

你的结构体有九个属性,输入的时候只有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
*/


修改如下,问题见注释行,供参考:

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

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632