Today I was learning about different types of data structures and I was trying to make a program that pops an element of a stack when prompted. However when I added a new text and input after the for loop, it only prints the last part of the stack.
Additionally I get this result when I type either the “y” or “n” prompt in for input:
System.Collections.Generic.Stack`1[System.String]
I also tried breaking my code down into pieces to try and see where the problem begins and I found that the write line and input after the for loop cause the issue with the stack now showing fully.
Also Here is the code in it’s entirety:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
Stack<string> wordActions = new Stack<string>();
wordActions.Push("resized text to 100");
wordActions.Push("recoloured text to red");
wordActions.Push("changed font to Courier New");
wordActions.Push("aligned text to center");
wordActions.Push("changed text to italic type");
foreach (string wordAction in wordActions) {
Console.WriteLine(wordAction);
Console.WriteLine("would you like to undo the last action? type y for yes and n for no");
string userInput =Console.ReadLine();
if (userInput == "y" | (userInput == "Y")) {
wordActions.Pop();
Console.WriteLine($"you have undone the last action which was CHANGED TEXT TO ITALIC TYPE. nhere is your list of commands now:");
Console.WriteLine(wordActions);
}
else if (userInput == "n" | (userInput == "N")) {
Console.WriteLine("No problem your list of actions is still as follows:");
Console.WriteLine(wordActions);
}
System.Environment.Exit(0);
}
}
}
Thank you for answers in advance!