如何通过方法重写正确显示栈整数值并解决垃圾值问题
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
myStackhas a constructor namedStack()instead ofmyStack()— this means thetopvaluevariable 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 astopvalue(e.g.,cout<<"topvalue = "<<valueinbox;), which is why you sawtopvalue=11instead 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
- Fixed Constructor: Renamed
Stack()tomyStack()in the base class, ensuringtopvaluestarts at-1(the correct initial state for an empty stack). This eliminates the garbage value issue entirely. - Corrected Output Logs: Adjusted print statements in
pushandpopto clearly show the stack’s state (top index) and the values being pushed/popped, so you can track operations easily. - Added
<cstdlib>: Included the header required forexit()to function properly across compilers. - Enhanced
popMethod: 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
相关产品推荐
相关产品推荐

