顺序栈的出栈问题,总是无法完全输出

顺序栈中,我想借用出栈来输出栈中的所有内容,但是总是不能输完全
//栈的存储结构
//顺序栈
#include<iostream>
using namespace std;

typedef struct node{
    int data[100];
    int top;//栈顶指针  
}SqStack;

//栈的初始化
void InitStack(SqStack &s){
    s.top=-1;
} 

//出栈
bool PopStack(SqStack &s,int &x){
    if(s.top==-1){
        return false;
    }
    x=s.data[s.top];
    s.top--;
    return true;    
} 

//入栈
bool PushStack(SqStack &s){
    int num,i;
    cout<<"请输入你要入栈的个数:";
    cin>>num;
    for(i=0;i<num;i++){
        if(s.top==99){
            return false;
        }
        
        cin>>s.data[++s.top];
    }
    return true;
} 

//判空
void isEmpty(SqStack &s){
    if(s.top==-1){
        cout<<"栈为空!"<<endl;
    }else{
        cout<<"栈不空。"<<endl;
    }
} 

//判满
void isFull(SqStack &s){
    if(s.top==99){
        cout<<"栈满"<<endl;
    }else{
        cout<<"栈未满"<<endl;
    }
} 

//取栈顶元素
void GetTop(SqStack &s){
    if(s.top==-1){
        return;
    }
    cout<<s.data[s.top]<<endl;    
} 

//取栈长
int GetLength(SqStack &s){
    return (s.top+1);
} 

int main(){
    int x,i;
    SqStack s;
    InitStack(s);
    PushStack(s);
    cout<<"\n栈顶元素:";
    GetTop(s);
    cout<<"\n栈的长度:";
    cout<<GetLength(s);
    cout<<"\n出栈:";
    
    for(i=0;i<GetLength(s);i++){
        PopStack(s,x);
        cout<<x<<"    ";
    }
    /*
    PopStack(s,x);
        cout<<x;
        PopStack(s,x);
        cout<<x;
        PopStack(s,x);
        cout<<x;
    */    
        
    cout<<endl;
    cout<<"判空:";
    isEmpty(s);
    
    
    return 0;
} 

运行结果及报错内容

img

我的解答思路和尝试过的方法

使用了while和for循环

我想要达到的结果

代码没什么问题,主函数里做了修改,出栈循环的逻辑有误,修改如下,供参考:

//栈的存储结构
//顺序栈
#include<iostream>
using namespace std;

typedef struct node{
    int data[100];
    int top;//栈顶指针
}SqStack;

//栈的初始化
void InitStack(SqStack &s){
    s.top=-1;
}

//出栈
bool PopStack(SqStack &s,int &x){
    if(s.top == -1){
        return false;
    }
    x=s.data[s.top];
    s.top--;
    return true;
}

//入栈
bool PushStack(SqStack &s){
    int num,i;
    cout<<"请输入你要入栈的个数:";
    cin>>num;
    for(i=0;i<num;i++){
        if(s.top==99){
            return false;
        }
        
        cin>>s.data[++s.top];
    }
    return true;
} 
 
//判空
void isEmpty(SqStack &s){
    if(s.top==-1){
        cout<<"栈为空!"<<endl;
    }else{
        cout<<"栈不空。"<<endl;
    }
} 
 
//判满
void isFull(SqStack &s){
    if(s.top==99){
        cout<<"栈满"<<endl;
    }else{
        cout<<"栈未满"<<endl;
    }
} 
 
//取栈顶元素
void GetTop(SqStack &s){
    if(s.top==-1){
        return;
    }
    cout<<s.data[s.top]<<endl;
} 
 
//取栈长
int GetLength(SqStack &s){
    return (s.top+1);
} 
 
int main(){
    int x,i;
    SqStack s;
    InitStack(s);
    PushStack(s);
    cout<<"\n栈顶元素:";
    GetTop(s);
    cout<<"\n栈的长度:";
    cout<<GetLength(s);
    cout<<"\n出栈:";
    
    for(i=0;PopStack(s,x);i++){//for(i=0;i<GetLength(s);i++){ 修改
        //PopStack(s,x);  修改
        cout<<x<<"    ";
    }
    /*
    PopStack(s,x);
        cout<<x;
        PopStack(s,x);
        cout<<x;
        PopStack(s,x);
        cout<<x;
    */    
        
    cout<<endl;
    cout<<"判空:";
    isEmpty(s);
    return 0;
}