0

I'm brand spanking new and apologize if the title doesn't even make sense. I've been pretty good at finding answers on here from what other people have asked, but I'm not sure about this one. If the user inputs anything other than the answer for an if statement (else) it gives the right response, but then any other key after that just closes the console. I'm making a "choose your own adventure" for my very first project and I am able to get the right responses when the user inputs an answer, but for the "else" statement, it gives the right response, but then if I hit any other key, it closes the whole console.

I'm making a "choose your own adventure" for my very first project and I am able to get the right responses when the user inputs an answer, but for the "else" statement, it gives the right response, but then if I hit any other key, it closes the whole console.

        //Prompt user for a choice

        Console.WriteLine("What Do You Say?");
        Console.WriteLine("1=\"What's wrong\" \n2=\"I have to get to school\"");
        Console.WriteLine("> ");
        string helpGirl = Console.ReadLine();
        //User Helped the Girl
        if (helpGirl == "1")
        {
            Console.WriteLine("You ask the girl what's wrong, she slowly looks up at you...");
            Console.ReadKey(true);
            Console.WriteLine("The girl slowly turns and points to the house, saying \"I left my bracelet inside that house, can you please grab it\"");
        }
        //User kept going to school
        else if (helpGirl == "2")
        {
            Console.WriteLine("You Die!");
        }
        else
        {
            Console.WriteLine("Please Choose 1 or 2");
        }
        Console.ReadKey();
    }

I'm assuming this is because its just going to the next line of ReadKey instead of reverting back to the if statement? Thanks in advance.

Mondor
  • 1

3 Answers3

0

I'm guessing that you are currently in the Main() method. The issue is that after the ReadKey call, it will get to the end and exit. Thus, you should wrap everthing in a while loop. You could do something like this:

void Main(...)
{
    var isRunning = true;
    while (isRunning)
    {
        ....
        var key = Console.ReadKey();
        if (key == 'Q')
           isRunning = false;
    }

}
Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39
0

Try this code:

 // Prompt user for a choice
        Console.WriteLine("What do you say?");
        Console.WriteLine("1 = \"What's wrong\" \n2 = \"I have to get to school\"");
        Console.Write("> ");

        // Loop until the user inputs a valid choice
        bool validChoice = false;
        while (!validChoice)
        {
            string helpGirl = Console.ReadLine();
            if (helpGirl == "1")
            {
                Console.WriteLine("You ask the girl what's wrong, she slowly looks up at you...");
                Console.ReadKey(true);
                Console.WriteLine("The girl slowly turns and points to the house, saying \"I left my bracelet inside that house, can you please grab it\"");
                validChoice = true; // set validChoice to true to exit the loop
            }
            else if (helpGirl == "2")
            {
                Console.WriteLine("You die!");
                validChoice = true; // set validChoice to true to exit the loop
            }
            else
            {
                Console.WriteLine("Please choose 1 or 2");
                Console.Write("> ");
            }
        }
        Console.ReadKey();
Mike
  • 114
  • 3
0

looks like you want to learn about For-Loops, While-Loops and Do-While-Loops

C# will flow through linearly to completion unless you have a loop surrounding your execution.

In this case, I would like elect for a Do-While-Loop so that you enter the loop once; and your while might be something like while(Console.ReadKey() != 'q');

Mike_Matthews_II
  • 722
  • 6
  • 15