0

I've been coding a text adventure game, and one of the random scenarios is where you can meet a pokémon and fight it. There are some options (like looking at inventory) that require you go back to the menu (fight, run, items, poké). For example, if you input a wrong command then you're supposed to go back to the menu. However, in the method, the only parameter is hasEncountered, and when the random scenario is triggered then it's false by default (bool), meaning

        if (hasEncountered == false)
        {
            Console.WriteLine("While travelling you go through a patch of tall grass..");
            Thread.Sleep(2000);
            Console.WriteLine("*pokemon music*");
            Thread.Sleep(1500);
            Console.Clear();
            Console.WriteLine("You encounter a {0}!", pokemon1);
        }

is triggered. Whenever a user inputs a wrong command, the method is recalled. I don't want the user to see this part again, so I did this:

        else
        {
            Console.WriteLine(" < {0} >", pokemon1);
        }

However, the string "pokemon1" in the if statement is undeclared, although I declared it in the beginning of the method.

    public void ConScen3(bool hasEncountered)
    {
        string pokemon1;
        TextAdventure1.ConjoinedScenarios.ConjoinedScenario conScen = new TextAdventure1.ConjoinedScenarios.ConjoinedScenario();
        if (hasEncountered == false)
        {
            Console.WriteLine("While travelling you go through a patch of tall grass..");
            Thread.Sleep(2000);
            Console.WriteLine("*pokemon music*");
            Thread.Sleep(1500);
            Console.Clear();
            Console.WriteLine("You encounter a {0}!", pokemon1);
        }
        else
        {
            Console.WriteLine(" < {0} >", pokemon1);
        }

If I try to declare pokemon1 in the if statement, it says it's already declared. However, if I remove the pokemon1 string in the beginning, the rest of the code complains that it's undeclared. Is there any way to fix this?

1
  • 2
    It's not complaining that it's undeclared, it's complaining that it has no value assigned. Commented Oct 5, 2013 at 10:06

2 Answers 2

1

You do declare the variable (that's what string pokemon1; does), but you don't assign a value to it.

You have to give the variable a value, like pokemon1 = "Foo";.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to assign a value to pokemon1:

public void ConScen3(bool hasEncountered)
    {
        string pokemon1 = "pikachu";
        TextAdventure1.ConjoinedScenarios.ConjoinedScenario conScen = new TextAdventure1.ConjoinedScenarios.ConjoinedScenario();
        if (hasEncountered == false)
        {
            Console.WriteLine("While travelling you go through a patch of tall grass..");
            Thread.Sleep(2000);
            Console.WriteLine("*pokemon music*");
            Thread.Sleep(1500);
            Console.Clear();
            Console.WriteLine("You encounter a {0}!", pokemon1);
        }
        else
        {
            Console.WriteLine(" < {0} >", pokemon1);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.