vscode c++调试

看别人c++数据结构时遇到问题:
这是线性表的初始化、插入、取值。
其实里面还有添加数据算法,合并到了插入算法。
两个疑问:1.L.length如何变化?2.调试报错?

#include
#include
#include
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define MAXSIZE 100
typedef struct
{
    ElemType *elem;
    int length;
}Sqlist;

Status InitList(Sqlist &L)
{//初始化
    L.elem=new ElemType[MAXSIZE];
    if(!L.elem)exit(OVERFLOW);
    L.length=0;
    return OK;
}
Status ListInsert(Sqlist &L,int i,ElemType e)
{//添加数据+插入
    if((i<1)||(i>L.length+1)) return ERROR;
    if(L.length==MAXSIZE) return ERROR;
    for(int j=L.length-1;j>=i-1;j--)
    L.elem[j+1]=L.elem[j];
    L.elem[i-1]=e;
    ++L.length;
    return OK;
}
Status GetElem(Sqlist &L,int i,ElemType &e)
{//取值
     if((i<1)||(i>L.length)) return ERROR;
     e=L.elem[i-1];
     return OK;
}
int main()
{
    //线性表的的初始化、插入、取值
    Sqlist Lscore; //用来存储学生《数据结构》成绩
    InitList(Lscore); //初始化线性表 
    int i;
    for(i=1; i<=5; i++)
    {
        ListInsert(Lscore,i,80+i); //线性表中添加数据 
    } 
    
    int s1,s2; //
    cin>>s1>>s2; //输入两个成绩 
    ListInsert(Lscore,3,s1);
    ListInsert(Lscore,5,s2);
    int len=Lscore.length;
    int s;
    for(i=1;i<=len;i++)
    {
        GetElem(Lscore,i,s); //获取线性表中数据 
        cout<return 0;
}


Status ListInsert(Sqlist &L,int i,ElemType e)
{//添加数据+插入
    if((i<1)||(i>L.length+1)) return ERROR;
    if(L.length==MAXSIZE) return ERROR;
    for(int j=L.length-1;j>=i-1;j--)
    L.elem[j+1]=L.elem[j];
    L.elem[i-1]=e;
    ++L.length;
    return OK;
}

问题1:
初始化时L.length=0;
这里添加数据时的L.length是多少?为什么?

img

img

问题2:
想调试一下,看length的值,发现报错,如何解决?

长度加1没毛病啊
你的代码测试了一下,没出现错误啊

vsc调试,文件路径中不要有中文。

之前有遇到过类似的问题,将文件改到英文地址就行

代码除了程序结束前没有释放内存外(要养成new和delete配对使用的习惯),其他的没有什么大问题,可以正常运行。
你遇到的问题,可能是文件名是中文的缘故,改成英文试试。

文件名改为英文试试?

img

问题1:
L.length值的变化情况如下:
初始化时为0
手动输入两个成绩之前是5
手动输入两个成绩之后是7
见运行截图

img

问题2:你保存代码的文件命名是“线性表的初始化、插入、取值.cpp”
把文件名字改成"LpList.cpp",保存,然后重新编译运行代码。