Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console Keypress not working?

So I'm making a console-based text game to learn more C# but I'm now stuck and have resorted to SO.

This might be an obvious answer but I require another pair of eyes to help me. I've checked the other questions on here and they don't seem to aid me.

In my Main(), I have the following:

        int age;

        Console.Write("Before entering the unknown, could you please confirm your age? ");
        age = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("");

        if (age < 18)
        {
            Console.WriteLine("I'm sorry, you'll now be transferred to a non-explicit version.");
            Console.WriteLine("\n<Press any key to continue>");
            Console.ReadKey();
            Console.Write("\nPlease wait...");
            for (int t = 5; t >= 0; t--)
            {
                Console.CursorLeft = 22;
                Console.Write("{0:00}" + " seconds remaining", t);
                Thread.Sleep(1000);
            }
            Console.WriteLine("");
            Console.WriteLine("\nTo proceed, please press any key...");
            Console.ReadLine();

            NonExplicit();
        }

Self explanatory. Once it hits the NonExplicit() method, the following is called:

        char yes = Console.ReadKey().KeyChar;
        string name;

        Console.WriteLine("\nYou're awake? It's about time. Not old enough, eh? That sucks. \n\nListen, I really need your help. Would you be interested? Y / N");
        Console.ReadKey();

        if (yes == 'y' || yes == 'Y')
        {
            Console.Write("\nYou will?! Wow, That's fantastic. Right then, where shall I start? \nI know! How about telling me your name? ");
            name = Console.ReadKey().ToString();
            Console.WriteLine("\nAhh yes, now you mention it, I certainly remember you, {0}!", name);   
        }
        else
        {
            Console.WriteLine("\nYou don't want to help me? Oh, that's unfortunate... ");
        }   

What seems to happen is that, when 'y' or 'Y' is pressed:

1) It requires enter to be hit to register the above.

2) It runs both the if & else WriteLines, which I assume is in conjunction with 1)?

How can I amend my code so that when 'y/Y' is pressed, it reads the correct criteria? Or how can I remove the need for Enter to be pressed to proceed?

like image 514
Sean Avatar asked Apr 17 '26 05:04

Sean


1 Answers

Used your code below, I tested it and it works.

I moved the char yes = Console.ReadKey() to below the Console.WriteLine().
I changed the name = Console.ReadKey() to Console.ReadLine() because a name is more then 1 key.

        string name;
        Console.WriteLine("\nYou're awake? It's about time. Not old enough, eh? That sucks. \n\nListen, I really need your help. Would you be interested? Y / N");
        char yes = Console.ReadKey();

        if (yes == 'y' || yes == 'Y')
        {
            Console.Write("\nYou will?! Wow, That's fantastic. Right then, where shall I start? \nI know! How about telling me your name? ");
            name = Console.ReadLine();
            Console.WriteLine("\nAhh yes, now you mention it, I certainly remember you, {0}!", name);
            Console.ReadKey(); // I put this here so the program wouldn't exit
        }
        else
        {
            Console.WriteLine("\nYou don't want to help me? Oh, that's unfortunate... ");
        }
like image 124
EpicKip Avatar answered Apr 19 '26 17:04

EpicKip