You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过方法重写正确显示栈整数值并解决垃圾值问题

Fixing Your Stack Override Code to Avoid Garbage Values & Correctly Display Output

Let’s break down what’s going wrong with your code and fix it step by step, so you can get the expected output for your stack operations:

Key Issues in the Original Code

  • Constructor Name Mismatch: Your base class myStack has a constructor named Stack() instead of myStack() — this means the topvalue variable never gets initialized to -1, leaving it with a random garbage value (like the 4249067 you saw). This immediately triggers the "stack full" error before you even push valid values.
  • Mixed-Up Output Statements: In childStack::push, you’re printing the input value as topvalue (e.g., cout<<"topvalue = "<<valueinbox;), which is why you saw topvalue=11 instead of the actual top index state.
  • Missing Required Header: The exit() function needs the <cstdlib> header to work reliably; omitting it can cause compilation issues on some compilers.

Fixed Code

#include<iostream>
#include<conio.h>
#include<cstdlib>  // Added for exit() functionality
using namespace std;

class myStack {
protected:
    enum { maximumnumberofboxes = 3 };
    int stack[maximumnumberofboxes];
    int topvalue;
public:
    myStack()  // Fixed constructor name to match class name
    { topvalue = -1; }

    void push(int valueinbox)
    { stack[++topvalue] = valueinbox; }

    int pop()
    { return stack[topvalue--]; }
};

class childStack : public myStack {
public:
    void push(int valueinbox)
    {
        if(topvalue >= maximumnumberofboxes - 1)
        {
            cout << "\nError: stack is full";
            exit(1);
        }
        // Corrected output to show actual stack state
        cout << "\nTop index before push: " << topvalue;
        cout << "\nPushing value: " << valueinbox;
        myStack::push(valueinbox);
        cout << "\nTop index after push: " << topvalue;
    }

    int pop()
    {
        if(topvalue < 0)
        {
            cout << "\nError: stack is empty\n";
            exit(1);
        }
        int popped_val = myStack::pop();
        cout << "\nPopped value: " << popped_val;
        cout << "\nTop index after pop: " << topvalue;
        return popped_val;
    }
};

int main() {
    childStack s1;
    s1.push(11);
    s1.push(22);
    s1.push(33);
    
    cout << "\n\n--- Starting Pop Operations ---";
    cout << endl << s1.pop();
    cout << endl << s1.pop();
    cout << endl << s1.pop();
    // Uncomment below to test empty stack error
    // cout << endl << s1.pop();

    cout << endl;
    system("pause");
    getch();
    return 0;
}

What Changed & Why

  1. Fixed Constructor: Renamed Stack() to myStack() in the base class, ensuring topvalue starts at -1 (the correct initial state for an empty stack). This eliminates the garbage value issue entirely.
  2. Corrected Output Logs: Adjusted print statements in push and pop to clearly show the stack’s state (top index) and the values being pushed/popped, so you can track operations easily.
  3. Added <cstdlib>: Included the header required for exit() to function properly across compilers.
  4. Enhanced pop Method: Added explicit output for popped values to make the stack behavior more transparent.

Expected Output

When you run the fixed code, you’ll see this clean output without garbage values:

Top index before push: -1
Pushing value: 11
Top index after push: 0

Top index before push: 0
Pushing value: 22
Top index after push: 1

Top index before push: 1
Pushing value: 33
Top index after push: 2

--- Starting Pop Operations ---
Popped value: 33
Top index after pop: 1
33
Popped value: 22
Top index after pop: 0
22
Popped value: 11
Top index after pop: -1
11
Press any key to continue . . .

All your target values (11, 22, 33) are pushed and popped correctly, and the stack state is clearly visible throughout the process.

内容的提问来源于stack exchange,提问作者Mohammed Sanwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:41:18