How do I make this code display the contents of the stack at the top of the text box.?
-
It is currently displaying the stack upside down, the contents of the top of the stack are displayed as the last line in the text box. I am trying to make this code display the contents of the stack at the top of the text box. :(( private void displayStack() { string nextItem; displayBox.Clear(); for (int i = 0; i <= stackTop; i++) { nextItem = stackArray[i].ToString(); displayBox.AppendText(nextItem); displayBox.AppendText("\n"); } }
-
It is currently displaying the stack upside down, the contents of the top of the stack are displayed as the last line in the text box. I am trying to make this code display the contents of the stack at the top of the text box. :(( private void displayStack() { string nextItem; displayBox.Clear(); for (int i = 0; i <= stackTop; i++) { nextItem = stackArray[i].ToString(); displayBox.AppendText(nextItem); displayBox.AppendText("\n"); } }
-
Just loop the other way.
Despite everything, the person most likely to be fooling you next is yourself.
-
private void displayStack() { string nextItem; displayBox.Clear(); // doing it the other way like guffa said for (int i = stacktop; i >= 0; i--) { nextItem = stackArray[i].ToString(); displayBox.AppendText(nextItem); displayBox.AppendText("\n"); } } instead of starting from bottom value of your stack,(originally your intial conditon is int i = 0) you are now starting at the max value of your stack. Instead of stopping the loop when your variable "i" reaches "stacktop" (top of your stack), the loop stopped at "i = 0"(bottom of your stack). Because of the above 2 statements, i++ have to change to i-- since you are starting from top to bottom now.
-
private void displayStack() { string nextItem; displayBox.Clear(); // doing it the other way like guffa said for (int i = stacktop; i >= 0; i--) { nextItem = stackArray[i].ToString(); displayBox.AppendText(nextItem); displayBox.AppendText("\n"); } } instead of starting from bottom value of your stack,(originally your intial conditon is int i = 0) you are now starting at the max value of your stack. Instead of stopping the loop when your variable "i" reaches "stacktop" (top of your stack), the loop stopped at "i = 0"(bottom of your stack). Because of the above 2 statements, i++ have to change to i-- since you are starting from top to bottom now.