-3

I just didn't find this in the Microsoft docs. I tried to use parameters inside brackets in Console.ReadKey();, but it isn't working.

I need to make the program terminate if the user presses a key other than the one specified in the program message. For example, the program requires the user to press the Enter key. If the user decides to press a different key, I want the program to terminate.

Example of code:

using System;

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Let's try to enter some number and show it in a console? (Press Enter/Return key to continue)");

            Console.ReadKey();

            Console.WriteLine("Enter your value");

            double x = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine($"Your value is {x}");

            Console.WriteLine("Press any key to exit");

            Console.ReadKey();
        }
    }
}
atiyar
  • 7,762
  • 6
  • 34
  • 75
  • What ***is*** "*right key*"... what does this mean "*I tried to use parameters inside brackets*"... Where is your code? In short this question is disappointingly vague – TheGeneral Feb 04 '21 at 04:38
  • what do you mean by the right key? – psj01 Feb 04 '21 at 04:38
  • [`bool intercept`](https://learn.microsoft.com/en-us/dotnet/api/system.console.readkey?view=net-5.0#System_Console_ReadKey_System_Boolean_) is the only parameter that `ReadKey` takes. Can you explain more what you're trying to do? – ProgrammingLlama Feb 04 '21 at 04:44
  • @00110001 haha yes, also mix up parameters vs arguments :-D https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter#:~:text=A%20parameter%20is%20a%20variable,pass%20into%20the%20method's%20parameters.&text=Parameter%20is%20variable%20in%20the,that%20gets%20passed%20to%20function. – Tomek Feb 04 '21 at 04:45
  • Hi, You have to use the x value and use an `if` construct to check it is desired value and do the action. `else` do what should be done – Ramesh Feb 04 '21 at 04:51
  • 1
    You're not using the result of `ReadKey()`. You should store the result and compare it to see if it corresponds to the key you want. – ProgrammingLlama Feb 04 '21 at 04:51
  • @Ramesh, how should this look in the code of my program? (.NET Core 3.1) At now we haven't learned this theme. We are learning arithmetical operations. – Niko Iwakura Feb 04 '21 at 04:53
  • Welcome to the awesome world of learning. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else is a good place to start `if` `else` concepts. Best Wishes – Ramesh Feb 04 '21 at 04:55
  • to add on @Ramesh answer, this is a free C# Programming e-book: https://introprogramming.info/english-intro-csharp-book/ – Tomek Feb 04 '21 at 06:10

3 Answers3

2
var pressedKey = Console.ReadKey();
if (pressedKey.KeyChar != '\r')
{
    Environment.Exit(0);
}
else
{
    continue;
}

The above code should exit the console app if any key except enter is pressed.

  • But I need to stop the program if any key except "Enter" is pressed. – Niko Iwakura Feb 04 '21 at 04:56
  • I am just a noob in programming. We are started learning to program a week or two ago and we haven't learned `if` and `else` operands(( – Niko Iwakura Feb 04 '21 at 05:01
  • Slight modification will be required for that, just replace if condition with this: if (pressedKey.KeyChar != '\r') – Purushottam.Prasad Feb 04 '21 at 05:03
  • 1
    @NikoIwakura your argument makes no sense?! You are asking for help, then argue that you haven't learned it yet. At some point you have to learn things ?! Also, rather than using SO, you really should read the MSDN or any C# programming book if you are new to programming... like: https://introprogramming.info/english-intro-csharp-book/ – Tomek Feb 04 '21 at 06:08
0

Try this -

Console.WriteLine("Let's try to enter some number and show it in a console? (Press Enter/Return key to continue)");

// exits if the key is not the Enter key
if (Console.ReadKey().Key != ConsoleKey.Enter)
    Environment.Exit(0);

Console.WriteLine("Enter your value");
double x = Convert.ToDouble(Console.ReadLine());
Console.WriteLine($"Your value is {x}");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
atiyar
  • 7,762
  • 6
  • 34
  • 75
-2

I have found a solution:

using System;

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Let's try to enter some number and show it in a console? (Press Enter/Return key to continue)");

            while (Console.ReadKey(true).Key != ConsoleKey.Enter);

            Console.WriteLine("Enter your value");

            double x = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine($"Your value is {x}");

            Console.WriteLine("Press any key to exit");

            Console.ReadKey();
        }
    }
}

It isn't making some things I needed, but it worx too

  • Why are you using the while loop here? This solution will not cause the application to terminate if you press `Q` instead of `Enter`. – atiyar Feb 04 '21 at 05:51