clion程序可运行,但会卡在scanf,后面程序无法执行,但未报错




#include 
//课10作业,顺序表的插入和删除
//针对顺序表定义结构体
#define MaxSize 50
typedef int ElemType;
typedef struct{
    ElemType  data[MaxSize] ;
    int length;
}SqList;

void ListInsert(SqList &L, int pos,ElemType InsertVal)
{//判断插入位置是否合法
    if(pos<1 || pos>L.length+1)
    {
        return;
    }
    if(L.length>=MaxSize)
    {
        return;
    }
    int j;
   for(j=L.length;j>=pos;j--)
   {
       L.data[j]=L.data[j-1];
   }L.data[pos]=InsertVal;
   L.length++;
}
void  PrintList(SqList L)
{
    int i;
   for(i=0;i<L.length;i++)
  {
    printf("%3d",L.data[i]);
  }
  printf("\n");
}

int main() {
    SqList L;
    L.data [0]=1;
    L.data[1]=2;
    L.data [2]=3;
    L.length=3;
    ElemType InsertVal;//或者是ElemType insert_val
   scanf("%d",&InsertVal); //读取要插入的元素
    ListInsert(L,2,InsertVal);//插入元素
    PrintList(L);

    return 0;
}


运行结果如下
D:\code\11\11.1-1sqlist\cmake-build-debug\11_1_1sqlist.exe
;实际应该运行结果为
D:\code\11\11.1-1sqlist\cmake-build-debug\11_1_1sqlist.exe
1 60 2 3
进程已结束,退出代码为 0
不知道问题出在哪里



scanf要求你输入值啊,根据你所说的正确结果,你应该输入60,按回车键后才能输出1 60 2 3 啊

插入函数修改下,供参考:

void ListInsert(SqList& L, int pos, ElemType InsertVal)
{//判断插入位置是否合法
    if (pos<1 || pos>L.length + 1)
    {
        return;
    }
    if (L.length >= MaxSize)
    {
        return;
    }
    int j;
    for (j = L.length; j >= pos - 1; j--) //for(j=L.length;j>=pos;j--) 修改
    {
        L.data[j] = L.data[j - 1];
    }
    L.data[pos - 1] = InsertVal;  //L.data[pos]=InsertVal;  修改
    L.length++;
}