I need help with this stack problem. I think I got it, but I dont.
Basically its a program to figure out who will be eaten by the dragon from a village. Names, and <CHOMPED> will be in the "villagers-in.txt" file. Every line of the file is supposed to be added to the stack, then if the line is equal to the string "<CHOMPED>", instead pop a name off of the stack and add it a generic "eaten" collection.
Ex.
Joe
Bob
<CHOMPED>
Ashley
...
public class VillageAndDragonProgram
{
public static void Main()
{
System.IO.StreamReader inFile = new System.IO.StreamReader("villagers-in.txt");
//Create generic stacks (That's the <T> kind!)
// ...
Stack<string> myStack = new Stack<string>();
String s;
while (true)
{
s = inFile.ReadLine();
foreach (string name in myStack)
{
myStack.Push(s);
}
if (s == "<CHOMP>")
{
Stack<string> pplEaten = new Stack<string>();
string chompED = myStack.Pop();
pplEaten.Push(chompED);
}
else
{
}
}
// Print out a file for the villager stack
//OutputCollectionToFile(myStack);
Console.ReadKey();
}
// Note that we're passing in IEnumerable -- we can "cast" any object as an
// object of its interface type, but then we can only access the methods
// exposed by the interface, in this case, acess to "foreach"
public static void OutputCollectionToFile(string fileName, IEnumerable myCollection)
{
using (System.IO.StreamWriter outFile = new System.IO.StreamWriter(fileName))
{
foreach (Object o in myCollection)
{
outFile.WriteLine(o);
}
outFile.Close();
}
}
}