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();
        }
    }
}

Recommended Answers

All 9 Replies

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>();
        Stack<string> pplEaten = new Stack<string>();

        String s;

        while (true)
        {
            s = inFile.ReadLine();

            foreach (string name in myStack)
            {
                myStack.Push(s);
            }
            if (s == "<CHOMP>")
            {
                //This initialisation must be done before while loop starts.
                //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();
        }
    }
}

Your problem is two fold, one solved by Hyperion's code. That problem is variable scope. In your original code, you create the pplEaten variable inside an if block, so when you exit the if block it no longer exists. Hyperion moved it to a scope that spans the entire problem.

The other problem, though, he left in. "<CHOMP>" tells you if you should push or pop off the stack, but you push everything onto the stack before checking what you should do. This results in "<CHOMP>" being pushed onto the stack, then immediately popped off the stack and added to your ppleEaten variable. What you need to do is check to see if it's "<CHOMP>" first, then decide if you should push or pop.

You need to change your while loop to:

String s;
s = infile.ReadLine();
while (s != null) {
    if (s == "<CHOMP>") {
        s = myStack.Pop();
        pplEaten.Push(s);
    } else {
        myStack.Push(s);
    }
    s = infile.ReadLine();
}

This would replace lines 13-36 in Hyperion's code, and also adds the ability to read more than one line from your file and end when it is done reading.

Hi guys, thanks alot for the help, really helped out. I'm also having a little problem calling the OutputCollectionToFile method. on line 36-40. Basically I need a outFile with the names left over from the stack that weren't eaten.
Im trying to use

OutputCollectionToFile(myStack, pplEaten);

but I get error- "No overload for method 'OutputCollectionToFile' takes '0' arguments

I'm not sure why you are getting that specific error, but it's because of the first parameter you are sending. The first parameter to that function is the name of the file you want to use, and you are sending it the myStack object instead.

yea method doesn't wana budge.
I've tried

OutputCollectionToFile(inFile, myStack);

and

OutputCollectionToFile(inFile, pplEaten);

and

OutputCollectionToFile(inFile, s);

nothing. There's nothing else to plug in! lol

None of the options you have used will work because OutputCollectionToFile takes two parameters:
1. of type string - Name of file :), whereas you are using inFile everywhere which of type StreamReader
2. of type IEnumerable

hmmm, so first parameter should be "villagers-in.txt", and im not sure what the second one would be...am I supposed to be casting something into IEnumerable???

OutputCollectionToFile(villagers-in.txt, ??)

As the project objective says, and extending it to the file, I am assuming you should pass "pplEaten" as second parameter.
This will generate a file with people eaten.

Alright, finally got it to work. Parameters were

OutputCollectionToFile("villagers-out.txt", s);

first parameter is basically a new .txt file, and s is the enumerable.
Great help guys. Thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.